← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Amazon SWE coding round, one algorithmic problem about graph traversal. Pretty focused session, no fluff.

Questions Asked (1)

Q1

Given a course catalog represented as an adjacency list, write a function that checks whether the catalog contains any cyclic dependencies between courses.

Algorithms & Data Structures
Author's notes

Cycle detection in a directed graph.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the course catalog as a directed graph and detect cycles using either DFS with recursion stack or Kahn's topological sort algorithm. Explain the chosen approach, analyze time and space complexity, and discuss handling edge cases like disconnected graphs and self-loops.

Pro tip: Mention that cycle detection in course prerequisites is equivalent to checking if a valid topological ordering exists, and that Kahn's algorithm naturally provides the ordering if no cycle is found—a useful bonus for scheduling.

1. Clarify the problem and graph representation

Confirm that the adjacency list represents directed edges from a course to its prerequisites (or vice versa) and that a cycle means a course depends on itself transitively. Discuss input constraints and edge cases.

2. Choose a cycle detection algorithm

Select either DFS with a recursion stack (colors: white, gray, black) or Kahn's topological sort (BFS with in-degree tracking). Briefly justify your choice based on simplicity or additional benefits.

3. Walk through the algorithm step-by-step

Explain how the algorithm works: for DFS, mark nodes as visiting and visited, and detect a back edge; for Kahn's, repeatedly remove nodes with in-degree zero and check if all nodes are processed.

4. Analyze complexity and edge cases

State time complexity O(V+E) and space complexity O(V+E) for both approaches. Mention handling of disconnected graphs, self-loops, and empty input.

5. Discuss extensions and trade-offs

Mention that Kahn's algorithm can also produce a topological order if no cycle exists, and that DFS can be adapted to find the actual cycle. Compare iterative vs recursive DFS for large graphs.

Key Points to Mention

  • Graph modeling: courses as vertices, prerequisites as directed edges
  • Cycle detection via DFS with recursion stack (back edge detection)
  • Kahn's algorithm (topological sort) using in-degree and queue
  • Time and space complexity: O(V+E) time, O(V+E) space
  • Handling disconnected graphs and self-loops
  • Trade-offs: DFS recursion depth vs Kahn's iterative nature

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