Graph Interview Problems
Most graph problems in interviews are one of four things in disguise: reachability (DFS or BFS), shortest path in an unweighted grid (BFS), ordering with dependencies (topological sort) or connecting groups (union-find). The skill is spotting which one, since the graph is often hidden in a grid or a list of pairs.
Practise building the adjacency list yourself, because that is where most bugs are. Then decide how you will mark visited nodes before you write the traversal.
Related
- Dynamic programming problems
- DSA: sliding window problems
- SDE-2 preparation plan
- Company interview roadmaps
Frequently asked questions
BFS or DFS for a grid problem?
BFS when you need the shortest number of steps, DFS when you only need to know whether something is reachable or to count connected regions.
When do I use union-find?
When you keep merging groups and repeatedly ask whether two items are in the same group, for example connected components or detecting a cycle in an undirected graph.