← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Amazon SWE interview with a graph/cycle detection problem. Pretty standard stuff for this type of role but I fumbled around a bit before landing on the right approach.

Questions Asked (1)

Q1

Given a number of courses and a list of prerequisite pairs, determine whether it's possible to complete all courses without getting stuck in a circular dependency. Return true if feasible, false otherwise.

Algorithms & Data Structures
Author's notes

My first instinct was just BFS and I kind of went with it before fully thinking through why.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the courses and prerequisites as a directed graph where an edge from course A to course B means A is a prerequisite for B. The problem reduces to detecting whether this graph contains a cycle. Use either Kahn's algorithm (BFS-based topological sort) or DFS with recursion stack to check for cycles; if a cycle exists, return false, otherwise true.

Pro tip: Clarify with the interviewer whether the graph is directed and whether prerequisites can be repeated or self-referential. Mention that you'd handle edge cases like no prerequisites or disconnected components, and discuss time/space complexity upfront to show thoroughness.

1. Understand the problem and clarify constraints

Restate the problem: given numCourses and prerequisite pairs, determine if all courses can be completed. Ask about input size, whether prerequisites are directed, and if there can be duplicate edges or self-loops.

2. Model as a directed graph

Represent courses as nodes (0 to numCourses-1) and each prerequisite pair [a, b] as a directed edge b -> a (b must be taken before a). Build an adjacency list and optionally an in-degree array.

3. Choose a cycle detection algorithm

Select either Kahn's algorithm (topological sort via BFS) or DFS with a recursion stack. Explain the trade-offs: Kahn's is iterative and easy to reason about; DFS can be more concise but requires careful state tracking.

4. Implement and trace through an example

Write clean code for the chosen algorithm. Walk through a small example (e.g., numCourses=2, prerequisites=[[1,0]]) to show it returns true, and a cyclic example (e.g., [[1,0],[0,1]]) to show it returns false.

5. Analyze complexity and discuss edge cases

State time complexity O(V+E) and space complexity O(V+E). Mention edge cases: no prerequisites, disconnected graph, self-loop, and large input. Optionally, discuss how to return the actual course order if needed.

Key Points to Mention

  • Directed graph representation: adjacency list and in-degree array
  • Cycle detection via topological sort (Kahn's algorithm) or DFS with recursion stack
  • Time and space complexity: O(V+E) where V is numCourses and E is number of prerequisites
  • Handling edge cases: empty prerequisites, disconnected components, self-loops, duplicate edges
  • Difference between directed and undirected cycle detection (undirected uses union-find or DFS with parent tracking)
  • Potential follow-up: return a valid course order (topological sort) instead of just boolean

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.