Graph Mastery 14-Day
Spot DFS vs BFS vs union-find vs Dijkstra at the problem statement, narrate the choice, and ship the solution without rebuilding the templates from scratch.
By Alex Chen, Founder, InterviewChamp.AI · 14 problems · ~22h · difficulty: mixed · Last verified
Grid DFS with visited marking. The 'count connected regions' template — every grid graph problem starts here.
Same shape as islands, different surface. Practice on the adjacency-list version — the data structure swap is the whole exercise.
Hash map old-to-new doubles as your visited set. The pattern: visited acts double-duty as the clone-lookup. Name it out loud.
Reverse the question: instead of 'can water reach the ocean from this cell?', BFS FROM each ocean. The inversion is the interview signal.
A tree is a connected graph with exactly n-1 edges and no cycles. Two checks, one pass. Or use union-find — both answers buy you points.
Union-find gateway. Process edges; the first one that unions two already-connected nodes is your answer. Memorize the union-find template now.
Topological sort via DFS-cycle-detection. The 'three colors' framing (unvisited, in-stack, done) is the answer interviewers want to hear.
Same engine, return the order. Kahn's BFS variant is cleaner here — practice both and pick one to defend.
BFS where 'neighbors' = one-letter-different words. The wildcard-bucket preprocessing trick is the unlock — don't compare every pair.
Union-find on emails. The trick: each email points to its account. Merge accounts whose emails overlap. The data modeling IS the problem.
Weighted graph + DFS. Each edge carries a ratio. Multiply along the path. First problem where edges have data beyond 'connected'.
Dijkstra template. Priority queue, distance map, relax edges. Drill the boilerplate — Cheapest-flights is the same engine.
Modified Dijkstra (or Bellman-Ford) with a stop counter in state. The classic 'state = (node, steps)' DP-graph hybrid.
Capstone. Build the graph from word pairs, then topological sort. The graph construction is the interview — the toposort is rote.
Ready to drill these live?
Get the AI copilot in your ear during real interviews. Real-time transcription. Streaming answers. Post-call scorecard.
Download the desktop app →