← Amazon Interview Insights

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

Intermediate
Jun 2026

Summary

Amazon SWE coding round, one algorithm question the whole time. Pretty standard if you've prepped graph problems, but the specific variant they picked tripped me up a bit.

Questions Asked (1)

Q1

Given a list of courses and their prerequisites, determine if it's possible to complete all courses (a topological sort / cycle detection variant).

Algorithms & Data Structures
Author's notes

Knew it was a graph problem pretty fast, but I fumbled the cycle detection part.

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 detect cycles using either Kahn's algorithm (BFS-based topological sort) or DFS with recursion stack. If a topological ordering exists, all courses can be completed; otherwise, a cycle indicates impossibility.

Pro tip: Clarify edge direction upfront (e.g., prerequisite → course) and mention that Kahn's algorithm naturally provides the order and detects cycles, which is often preferred in interviews for its iterative nature and easy complexity analysis.

1. Clarify and Model the Problem

Confirm input format (e.g., number of courses and list of prerequisite pairs) and define the graph: nodes are courses, directed edges represent prerequisites. Ask if there are any constraints like disconnected components or self-loops.

2. Choose an Algorithm

Select either Kahn's algorithm (BFS with in-degree tracking) or DFS with cycle detection. Explain why one is preferred (e.g., Kahn's avoids recursion depth issues and directly yields a topological order).

3. Walk Through the Algorithm

Describe the steps: for Kahn's, compute in-degrees, enqueue nodes with in-degree 0, process until queue empty, decrement in-degrees of neighbors. For DFS, mark nodes as unvisited, visiting, or visited, and detect back edges.

4. Analyze Complexity and Edge Cases

State time and space complexity: O(V+E) for both approaches. Discuss edge cases: empty input, no prerequisites, self-loop, disconnected graph, and large input sizes.

5. Conclude and Optimize

Conclude whether all courses can be completed based on cycle detection. Mention potential optimizations like early termination if a cycle is found or using iterative DFS to avoid stack overflow.

Key Points to Mention

  • Graph representation: adjacency list for efficiency with sparse graphs.
  • Cycle detection: a directed graph has a topological order iff it is a DAG (no cycles).
  • Kahn's algorithm: uses in-degree and queue; if processed nodes < total nodes, a cycle exists.
  • DFS approach: use three states (unvisited, visiting, visited) to detect back edges.
  • Time and space complexity: O(V+E) time, O(V+E) space for both algorithms.
  • Edge cases: self-loops, disconnected components, and empty input.

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