DFS & BFS Practice Questions
Ten interview classics. Each one is “DFS or BFS plus a small twist.” Recognizing which algorithm to use is half the work.
For each problem, before peeking at the solution, ask:
- Shortest / minimum steps? → BFS.
- Count / detect / enumerate? → DFS.
- Spread from multiple starts simultaneously? → multi-source BFS.
Most of these problems have nicknames in the interview circuit. Recognize them and the solution writes itself.
Medium
| Problem | Pattern | Status |
|---|---|---|
| Number of Islands | Grid DFS — count component starts | Available |
| Max Area of Island | Grid DFS — aggregate during traversal | Available |
| Clone Graph | DFS / BFS + hash-map memo | Available |
| Course Schedule | Directed cycle detection (DFS or BFS) | Available |
| Is Graph Bipartite? | BFS 2-coloring | Available |
| Surrounded Regions | Border-anchored DFS | Available |
| Pacific Atlantic Water Flow | Multi-source DFS (reverse-flood) | Available |
| Walls and Gates | Multi-source BFS | Available |
| Shortest Path in Binary Matrix | BFS with 8 directions | Available |
Hard
| Problem | Pattern | Status |
|---|---|---|
| Word Ladder | BFS on word transformations | Available |
More Practice (Coming Soon)
| Problem | Pattern | Status |
|---|---|---|
| Course Schedule II | Topological sort (Kahn’s) | Coming Soon |
| Open the Lock | BFS on state space | Coming Soon |
| Word Ladder II | BFS + path reconstruction | Coming Soon |
| Minimum Number of Days to Make Bouquets | Binary search + BFS | Coming Soon |
| 01 Matrix | Multi-source BFS | Coming Soon |
| Shortest Bridge | DFS to find one island + BFS | Coming Soon |
| Cheapest Flights Within K Stops | Modified BFS / Bellman-Ford | Coming Soon |
| Redundant Connection | Union-Find | Coming Soon |