What is Tarjan's algorithm for finding Strongly Connected Components?

Why Interviewers Ask This

This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.

Answer

Tarjan's algorithm finds all Strongly Connected Components (SCCs) in a directed graph in O(V+E). An SCC is a maximal subset of vertices where every vertex is reachable from every other. Key concepts: DFS discovery time (disc[u]): when u is first visited; low[u]: minimum disc value reachable from the subtree rooted at u (including back edges). Algorithm: run DFS; maintain a stack of visited nodes; for each node u: set disc[u] = low[u] = current time; push to stack; for each neighbor v: if unvisited, recurse, low[u] = min(low[u], low[v]); if v is on stack, low[u] = min(low[u], disc[v]); after processing all neighbors: if low[u] == disc[u], u is the root of an SCC — pop the stack until u and output as one SCC. Time: O(V+E). Each node is pushed and popped at most once. Kosaraju's algorithm (alternative): (1) DFS on original graph, record finish order; (2) Transpose the graph (reverse all edges); (3) DFS on transposed graph in reverse finish order. Each DFS on step 3 gives one SCC. Also O(V+E) but requires two DFS passes. Applications: (1) Finding cycles in directed graphs; (2) Condensation DAG (reduce SCCs to single nodes for topological sort); (3) Compiler optimization; (4) Web page groupings.

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.