← Amazon Interview Insights

Amazon·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Amazon SWE coding round, got a graph problem that looked like the classic course scheduling thing. Pretty standard if you know your topological sort, but the cycle detection part is where I think they're actually separating candidates.

Questions Asked (1)

Q1

Given a number of courses and a list of prerequisite pairs, return a valid ordering in which all courses can be completed. If a cycle exists making this impossible, return an empty array.

Algorithms & Data Structures
Author's notes

I knew Kahn's algorithm going in so I wasn't totally lost, but I second-guessed myself on the cycle detection part and spent way too long explaining it before writing any code.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the courses and prerequisites as a directed graph and use topological sorting to find a valid order. Detect cycles by checking if the number of processed nodes equals the total number of courses; if not, return an empty array.

Pro tip: At Amazon, emphasize scalability and robustness: mention that your solution handles large inputs efficiently and that you validate the input for edge cases like duplicate prerequisites or self-loops.

1. Clarify requirements and edge cases

Ask clarifying questions about input format, constraints, and expected output. Confirm whether the graph is directed, if there can be multiple valid orders, and how to handle invalid inputs.

2. Choose the right algorithm

Decide between Kahn's algorithm (BFS-based) and DFS-based topological sort. Both are O(V+E) time and space, but Kahn's is often easier to explain and implement iteratively.

3. Implement topological sort with cycle detection

Build the graph and compute in-degrees. Use a queue to process nodes with zero in-degree, appending to the result. If the result size is less than the number of courses, a cycle exists.

4. Test with examples and edge cases

Walk through simple cases (no prerequisites, linear chain), a cycle case, and a disconnected graph. Verify that the algorithm returns a valid order or an empty array as appropriate.

5. Analyze complexity and discuss trade-offs

State that the time and space complexity are O(V+E). Mention that DFS can also detect cycles but may require recursion stack management; Kahn's algorithm is iterative and avoids recursion limits.

Key Points to Mention

  • Directed graph representation: adjacency list and in-degree array
  • Topological sorting: definition and real-world analogy (course scheduling)
  • Kahn's algorithm: BFS with queue, processing nodes with zero in-degree
  • Cycle detection: if processed nodes < total nodes, cycle exists
  • Time and space complexity: O(V+E) where V is courses and E is prerequisites
  • Edge cases: empty input, no prerequisites, multiple valid orders, self-loop

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