Coordinate navigation, spiral unwrapping, and 2D cellular automations
250 problems·37 Easy·149 Medium·64 Hard
Pattern Study Guide & Cheat Sheet· O(R * C) visits each grid cell at most a constant number of times · O(R * C) for queue/recursion stack in worst case
▼
Navigate 2D grids as implicit graphs where each cell (r, c) has up to 4 orthogonal neighbors, using direction arrays [(0,1), (0,-1), (1,0), (-1,0)] and boundary checks.
Core Invariant: Grid = Graph where V = R × C and E ≤ 4 × R × C. Use direction vectors `DIRECTIONS = [(0, 1), (0, -1), (1, 0), (-1, 0)]` to replace 4 repetitive if-statements with a single clean loop.
Recognize it (Keywords & Signals)
Number of islands / island perimeter / max area of island
Flood fill / paint bucket tool
Rotting oranges (multi-source BFS)
Surrounded regions / capture enclosed territory
Pacific Atlantic water flow
When NOT to use
Grid has arbitrary jump teleports or diagonal movement when only orthogonal is specified.
3.Centralize boundary check: `0 <= r < ROWS and 0 <= c < COLS and grid[r][c] == target`.
4.For DFS/BFS: mark visited immediately (e.g. mutate `grid[r][c] = "0"` in-place or add to `visited` set).
5.For multi-source BFS: enqueue all starting sources (e.g. all rotten oranges) at time 0 before running queue.
Watch for (Interview Traps)
Mixing up `ROWS` and `COLS` in boundary checks (`r < COLS` or `c < ROWS`)
Mutating grid without confirming if mutation is allowed in interview problem statement
In multi-source BFS: incrementing time counter even when no fresh items were converted
Multi-Source Grid BFS (Rotting Oranges)
from collections import deque
def oranges_rotting(grid: list[list[int]]) -> int:
ROWS, COLS = len(grid), len(grid[0])
queue = deque()
fresh_count = 0
# 1. Multi-source initialization: find all rotten oranges and count fresh
for r in range(ROWS):
for c in range(COLS):
if grid[r][c] == 2:
queue.append((r, c))
elif grid[r][c] == 1:
fresh_count += 1
if fresh_count == 0:
return 0
minutes = 0
DIRECTIONS = [(0, 1), (0, -1), (1, 0), (-1, 0)]
# 2. BFS spreading level by level
while queue and fresh_count > 0:
minutes += 1
for _ in range(len(queue)):
r, c = queue.popleft()
for dr, dc in DIRECTIONS:
nr, nc = r + dr, c + dc
if 0 <= nr < ROWS and 0 <= nc < COLS and grid[nr][nc] == 1:
grid[nr][nc] = 2 # Infect fresh orange
fresh_count -= 1
queue.append((nr, nc))
return minutes if fresh_count == 0 else -1
Cost
O(R * C) visits each grid cell at most a constant number of times · O(R * C) for queue/recursion stack in worst case (Can mutate grid in-place to achieve O(1) auxiliary heap space.)
Canonical problems
#200 Number of Islands:Iterate cells; whenever "1" found, trigger DFS to sink connected land
#994 Rotting Oranges:Multi-source BFS tracks time for rot to propagate across grid
#130 Surrounded Regions:Start DFS from boundary "O"s to preserve non-enclosed regions
#417 Pacific Atlantic Water Flow:Reverse flow: DFS upwards from Pacific and Atlantic borders
What is the Matrix & Grid Traversal pattern in coding interviews?
Coordinate navigation, spiral unwrapping, and 2D cellular automations It is categorized under Trees & Graphs and features 250 curated interview problems across Easy (37), Medium (149), and Hard (64) difficulties.
Which tech companies frequently test Matrix & Grid Traversal?
How do I master the Matrix & Grid Traversal pattern?
Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.