← Questions
Detect a cycle in a directed graph
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Uber's technical phone screen and at Amazon, last asked Dec 2025. It tests graphs, dfs and cycle detection.
Detect cycles in linked structures, in three steps that build on each other.
Part 1: a linked list
Unless you agree on a different interface with the interviewer, assume:
-
has_cycle(head: Node | None) -> bool— each node has onenext. Return True if followingnextever revisits a node.
Part 2: a directed graph
Each node may now point to several children.
-
graph_has_cycle(n: int, edges: list[tuple[int, int]]) -> bool— nodes0..n-1; edge(u, v)goes fromutov.
Part 3: checking a new edge
A system stores items and directed links between them, and the links are guaranteed to form no cycle. Before saving a new link, check that it keeps the graph acyclic.
-
can_add_edge(n: int, edges: list[tuple[int, int]], new_edge: tuple[int, int]) -> bool— True if addingnew_edgekeeps the graph acyclic.
Example
list 1 -> 2 -> 3 -> 1 has_cycle -> True
graph_has_cycle(3, [(0, 1), (1, 2), (2, 0)]) -> True
graph_has_cycle(4, [(0, 1), (0, 2), (1, 3), (2, 3)]) -> False # 3 is reached twice, but no cycle
can_add_edge(3, [(0, 1), (1, 2)], (2, 0)) -> False # 0 already reaches 2
can_add_edge(3, [(0, 1)], (1, 2)) -> True
Constraints
- Up to 10^5 nodes and 10^5 edges, so aim for near-linear time.
Clarifying questions
- Can the graph have self-loops or repeated edges?
- In Part 3, is the existing graph guaranteed acyclic? (Yes.)
- Will many edges be checked one after another against the same graph?
What a strong answer covers
- Part 1: Floyd's fast and slow pointers, O(n) time and O(1) space, and how to find where the cycle starts.
- Part 2: depth-first search with three node states (unvisited, on the current path, finished), where an edge to a node on the current path is a cycle; or Kahn's algorithm, where a cycle exists if not every node can be removed. O(V + E). Why a plain visited set is not enough in a directed graph (the example with node 3).
- Part 3: adding
u -> vcreates a cycle exactly whenuis already reachable fromv, so one search fromvanswers it in O(V + E); a self-loopu == valways fails. - Iterative search to avoid recursion limits at 10^5 nodes.
Follow-ups
- Many edges are added over time, each checked before it is saved. Do better than a full search each time (keep a topological order and repair it locally).
- Return the cycle itself, not just whether one exists.
- The graph is undirected. What changes?
Where it's asked
- Uber · Technical phone screen · Last asked Dec 202560 min · One medium problem with a follow-up, or two short ones; sometimes object-oriented design
- Amazon · Technical phone screen · Last asked Oct 202560 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI
What it tests
graphs · dfs · cycle detection
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding

