← Amazon Interview Insights

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

Intermediate
Jun 2026

Summary

Amazon SWE coding round, one question the whole time: graph cycle detection plus topological sort. Pretty standard stuff but the implementation details can trip you up if you're not careful.

Questions Asked (1)

Q1

Given n courses and m prerequisite pairs, detect whether a valid course ordering exists (no cycles), and if so output one valid topological ordering of all courses.

Algorithms & Data Structures
Author's notes

Kahn's algorithm with in-degree tracking is probably the cleaner approach here since you get cycle detection for free when the queue empties before processing all nodes.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the courses as a directed graph where edges represent prerequisites, then use Kahn's algorithm (BFS-based topological sort) to detect cycles and produce an ordering. If the ordering includes all courses, return it; otherwise, report that no valid ordering exists due to a cycle.

Pro tip: Mention that Kahn's algorithm naturally detects cycles by comparing the output size to n, and that it's often preferred over DFS for its iterative nature and easier cycle detection. Also, discuss how to handle large inputs efficiently with O(V+E) time and space.

1. Clarify the problem and edge cases

Confirm that courses are labeled 0 to n-1, prerequisites are given as pairs [a, b] meaning b must be taken before a, and that multiple valid orderings may exist. Ask about input size constraints and whether the graph is guaranteed to be connected.

2. Build the graph and compute in-degrees

Create an adjacency list for the directed graph and an array to track the in-degree (number of prerequisites) for each course. Iterate through the prerequisite pairs to populate both.

3. Initialize queue with zero in-degree courses

Add all courses with in-degree 0 to a queue (or list) as they have no prerequisites and can be taken first.

4. Process queue and build topological order

While the queue is not empty, dequeue a course, add it to the result order, and for each of its neighbors, decrement their in-degree. If any neighbor's in-degree becomes 0, enqueue it.

5. Check for cycles and return result

After processing, if the result order contains all n courses, return it as a valid ordering. Otherwise, a cycle exists, so return an empty array or indicate impossibility.

Key Points to Mention

  • Topological sorting is only possible for Directed Acyclic Graphs (DAGs).
  • Kahn's algorithm uses BFS and in-degree tracking; DFS with recursion stack is an alternative.
  • Cycle detection: if the topological order doesn't include all nodes, a cycle exists.
  • Time complexity: O(V + E) where V is number of courses and E is number of prerequisites.
  • Space complexity: O(V + E) for adjacency list and in-degree array.
  • Handling disconnected graphs: the algorithm naturally processes all components.

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