What is BFS?
Breadth-First Search explores all neighbors at the current depth before moving to nodes at the next depth level. It uses a queue and guarantees the shortest path in unweighted graphs.Quick Recognition: Shortest path in unweighted graphs, level-order traversal, finding nearest target. Keywords: “minimum steps”, “shortest path”, “level by level”.
Pattern Recognition Checklist
Use BFS When
- Finding shortest path (unweighted)
- Level-order tree traversal
- Finding nearest target
- State space exploration (puzzles)
- Multi-source shortest distances
Don't Use When
- Weighted graph (use Dijkstra)
- Need to explore deep paths first
- Memory is very constrained
- Preorder/inorder/postorder needed
BFS vs DFS Quick Reference
| Aspect | BFS | DFS |
|---|---|---|
| Data Structure | Queue (FIFO) | Stack/Recursion (LIFO) |
| Shortest Path | Yes (unweighted) | No |
| Space Complexity | O(width) | O(height) |
| Use Case | Nearest, level-order | Paths, cycles, connectivity |
| Traversal Order | Level by level | Deep first |
When to Use
Shortest Path
Finding minimum steps in unweighted graphs
Level-Order Traversal
Processing tree/graph by levels
Nearest Neighbor
Finding closest target from source
State Space Search
Puzzle solving, game states
Pattern Variations
1. Tree Level-Order Traversal
2. Graph BFS (Shortest Path)
3. Grid BFS
4. Multi-Source BFS
5. Word Ladder (State Space BFS)
Classic Problems
1. Binary Tree Level Order Traversal
1. Binary Tree Level Order Traversal
Pattern: Process nodes level by levelKey: Track level size before processing
2. Rotting Oranges
2. Rotting Oranges
Pattern: Multi-source BFS with time trackingKey: Start from all rotten oranges simultaneously
3. Word Ladder
3. Word Ladder
Pattern: State space BFSKey: Each word is a state, edges are single-char changes
4. 01 Matrix
4. 01 Matrix
Pattern: Multi-source BFS from target cellsKey: Start from 0s to find distance to nearest 0
Common Mistakes
Interview Problems by Company
- Medium
- Hard
| Problem | Company | Key Concept |
|---|---|---|
| Level Order Traversal | All FAANG | Tree BFS |
| Rotting Oranges | Amazon, Meta | Multi-source BFS |
| Number of Islands | All FAANG | Grid BFS/DFS |
| Open the Lock | State space BFS | |
| Shortest Path in Matrix | Meta | Grid BFS |
Interview Tips
How to Explain Your Approach
How to Explain Your Approach
Script for interviews:
- “I need shortest path in unweighted graph, so I’ll use BFS.”
- “I’ll use a queue starting from the source.”
- “For each level, I process all current nodes before moving deeper.”
- “I mark nodes visited when enqueuing to avoid duplicates.”
- “Time is O(V + E), space is O(V) for the queue.”
BFS Template
BFS Template
Multi-Source BFS
Multi-Source BFS
For problems like “Rotting Oranges” or “01 Matrix”:
- Add ALL sources to initial queue
- Process level by level
- All sources expand simultaneously
- First to reach a cell = shortest distance
Practice Problems
| Problem | Difficulty | Link |
|---|---|---|
| Binary Tree Level Order | Medium | LeetCode 102 |
| Rotting Oranges | Medium | LeetCode 994 |
| Word Ladder | Hard | LeetCode 127 |
| Shortest Path in Binary Matrix | Medium | LeetCode 1091 |
| Open the Lock | Medium | LeetCode 752 |
Practice Roadmap
Day 2: Grid BFS
- Solve: Number of Islands, Rotting Oranges
- Focus: 4-directional movement, multi-source
Day 3: State Space BFS
- Solve: Open the Lock, Minimum Genetic Mutation
- Focus: State as node, transformation as edge