← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Amazon SWE interview with a graph cycle detection problem. Pretty standard coding round, not much else to say.

Questions Asked (1)

Q1

Given a graph, detect whether it contains a cycle.

Algorithms & Data Structures
Author's notes

Knew the general idea but fumbled a bit deciding between DFS with a visited set versus union-find.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify whether the graph is directed or undirected, as the cycle detection algorithm differs. For undirected graphs, use Union-Find or DFS with parent tracking; for directed graphs, use DFS with recursion stack or topological sort (Kahn's algorithm). Explain the chosen approach, its time and space complexity, and handle edge cases like disconnected graphs and self-loops.

Pro tip: Always discuss trade-offs between approaches: Union-Find is great for undirected graphs with dynamic edge additions, while DFS is simpler for static graphs. Mention that for directed graphs, topological sort can also detect cycles and is useful if you need a topological order.

1. Clarify graph properties

Ask if the graph is directed or undirected, and if it's represented as an adjacency list or matrix. This determines the algorithm choice.

2. Choose algorithm

For undirected: Union-Find or DFS with parent tracking. For directed: DFS with recursion stack or Kahn's algorithm. Explain why the chosen method fits.

3. Outline algorithm steps

Describe the algorithm concisely: e.g., for DFS, mark nodes as unvisited, visiting, visited; if a visiting node is encountered, a cycle exists.

4. Analyze complexity

State time and space complexity: typically O(V+E) time and O(V) space for DFS/Union-Find.

5. Handle edge cases

Mention disconnected graphs, self-loops, parallel edges, and empty graphs. Ensure the algorithm covers all components.

Key Points to Mention

  • Difference between directed and undirected cycle detection
  • Union-Find (Disjoint Set) with path compression and union by rank
  • DFS with recursion stack (colors: white, gray, black)
  • Kahn's algorithm (topological sort) for directed graphs
  • Time and space complexity: O(V+E) time, O(V) space
  • Handling disconnected graphs by iterating over all vertices

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