← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026Remote

Summary

Amazon SWE coding round, one graph problem that looked straightforward until I actually had to implement it cleanly under pressure.

Questions Asked (1)

Q1

Given a number of courses and a list of prerequisite pairs, return a valid order to complete all courses. If no valid order exists due to a cycle, return an empty array.

Algorithms & Data Structures
Author's notes

I knew this was topological sort the second they said it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the courses and prerequisites as a directed graph where edges point from prerequisite to dependent course. Use topological sorting (Kahn's algorithm or DFS) to find a linear ordering; if a cycle is detected, return an empty array. Clearly explain the algorithm, its time and space complexity, and how you handle edge cases.

Pro tip: At Amazon, emphasize scalability and robustness: mention that your solution handles large inputs efficiently and that you validate the input (e.g., duplicate edges, self-loops) before processing. Also, briefly discuss how you would test the solution with cycles and disconnected components.

1. Clarify and Model the Problem

Confirm input format and constraints (e.g., number of courses, prerequisite pairs). Model the problem as a directed graph where nodes are courses and edges represent prerequisites.

2. Choose an Algorithm

Decide between Kahn's algorithm (BFS-based) or DFS-based topological sort. Explain why you chose one (e.g., Kahn's is iterative and easy to detect cycles).

3. Implement Topological Sort

For Kahn's: compute in-degrees, use a queue to process nodes with zero in-degree, and build the order. For DFS: perform post-order traversal and detect back edges.

4. Detect Cycles and Handle Edge Cases

If the topological order doesn't include all courses, a cycle exists—return an empty array. Also handle cases like no prerequisites, disconnected components, and duplicate edges.

5. Analyze Complexity and Test

State time complexity O(V+E) and space complexity O(V+E). Walk through a small example and a cycle example to verify correctness.

Key Points to Mention

  • Graph representation: adjacency list for efficiency
  • Topological sorting algorithms: Kahn's (BFS) vs. DFS
  • Cycle detection: in-degree count or DFS back edge
  • Time and space complexity: O(V+E)
  • Handling edge cases: empty input, no prerequisites, cycles, disconnected graphs
  • Scalability and real-world application (e.g., course scheduling at Amazon)

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