Top 15 Matrix and Grid LeetCode Problems: What Each Teaches

- Grid problems are graph problems in disguise — every matrix question maps to one of five families: connected-region DFS, multi-source BFS, boundary DFS, grid DP, or in-place manipulation
- Number of Islands (LeetCode 200) is the canonical entry point — if you can't write connected-component DFS from memory in five minutes, the rest won't stick
- Multi-source BFS (Rotting Oranges, 01 Matrix) seeds the queue with every starting cell at once, cutting time from O(m²n²) to O(mn) — most candidates don't learn this until they see it in a live interview
- Boundary DFS (Surrounded Regions, Pacific Atlantic) always works backwards from the answer — start from border cells and find what to preserve, not what to eliminate
- In-place encoding tricks (Set Matrix Zeroes, Game of Life, Rotate Image) encode state transitions inside the matrix itself; the constant-space follow-up is nearly universal in real interviews
- Grid DP follows one shape every time: fill left-to-right, top-to-bottom using already-computed neighbors — Unique Paths is the foundation, Minimum Path Sum adds costs
- Name the pattern family before touching the keyboard — articulating your approach first is what Google and Amazon interviewers specifically score on grid problems
You get a grid problem in an interview. Your brain immediately asks: is this the one where I use some exotic 2D algorithm I never learned? It's not. Grid problems are graph problems in disguise. The 2D layout looks foreign until you realize every one maps onto something you already know: DFS, BFS, dynamic programming, backtracking, or in-place manipulation. The grid is just packaging.
This list covers 15 matrix and grid LeetCode problems in order of increasing complexity. Each teaches a distinct technique. Master the technique and the variants write themselves.
Five Families, Every Grid Problem
Everything in this list belongs to one of five families:
- Connected regions: DFS or BFS to explore and mark cells (islands, flood fill)
- Multi-source BFS: multiple starting cells spreading outward simultaneously
- Boundary DFS: start from the edges and work inward
- Grid DP: accumulate values along paths from a corner
- In-place tricks: encode state inside the matrix itself, no extra space
Learning to recognize which family you're in is the entire skill. Every grid problem you will ever see is a variation on one of these themes. Yes, even that cursed one at the end of the hard section.
1. Number of Islands (LeetCode 200)
Difficulty: Medium. Pattern: DFS/BFS connected components.
A grid of '1's and '0's. Count connected groups of land. Iterate every cell; when you find an unvisited '1', increment the count and flood-fill the entire connected component.
This is the canonical grid problem. Every other connected-region problem is a variant. If you can't write it from memory in five minutes, stop reading this list and go drill it. The rest of this won't stick without it.
2. Max Area of Island (LeetCode 695)
Difficulty: Medium. Pattern: DFS with accumulator.
Same grid as 200, but return the size of the largest island instead of the count. Your DFS returns the size of the component it explored.
The DFS does useful work rather than just marking. That shift from "marker DFS" to "accumulator DFS" appears in dozens of tree and graph problems. The depth-first search guide covers the mechanics.
3. Rotting Oranges (LeetCode 994)
Difficulty: Medium. Pattern: Multi-source BFS.
A grid holds fresh oranges, rotten oranges, and empty cells. Each minute, rot spreads to four neighbors. How many minutes until every fresh orange rots?
The trap: seed BFS with one rotten orange, run for a while, then pick up the next one. That gets the timing wrong because rot spreads from all rotten cells at the same time.
Seed your queue with every rotten cell at once and run BFS level by level. Each level is one minute. If any fresh orange is unreachable, return -1. This is the cleanest intro to multi-source BFS, a pattern most candidates see for the first time in a live interview at the worst possible moment.
4. 01 Matrix (LeetCode 542)
Difficulty: Medium. Pattern: Multi-source BFS for distances.
For every cell in a binary matrix, find the distance to the nearest 0. BFS from each 1 separately runs O(m²n²). Multi-source BFS from all 0 cells at once runs O(mn).
The key move: flip the search direction, starting from the destinations instead of the sources. See the BFS algorithm for the full mechanics.
5. Surrounded Regions (LeetCode 130)
Difficulty: Medium. Pattern: Boundary DFS, reverse thinking.
Any 'O' not connected to the grid border gets flipped to 'X'. The obvious approach: find every 'O' and check if it touches a border. This works, and it's slow, and it's also how you tell an interviewer you've never seen this pattern before.
DFS from every border 'O' to mark everything safe, then flip everything unmarked. Solve for what to preserve, not what to eliminate. The same logic appears in Pacific Atlantic, enclaves, and several harder variants.
6. Pacific Atlantic Water Flow (LeetCode 417)
Difficulty: Medium. Pattern: Multi-boundary DFS.
Find every cell that can drain to both the Pacific (top/left border) and Atlantic (bottom/right border). Brute-force DFS from every cell is too slow.
Reverse the flow: DFS from Pacific border cells, DFS from Atlantic border cells, return the intersection. Two passes, each O(mn). Practice this alongside Surrounded Regions until the backwards-search instinct is automatic.
7. Shortest Path in Binary Matrix (LeetCode 1091)
Difficulty: Medium. Pattern: BFS shortest path, 8-directional.
Find the shortest clear path from top-left to bottom-right. 0 is open, 1 is blocked. Movement in all eight directions, including diagonals.
BFS gives shortest paths in unweighted graphs, and a grid is just a graph. Diagonal movement widens the direction array from four entries to eight. That's the only structural change.
8. Word Search (LeetCode 79)
Difficulty: Medium. Pattern: DFS with backtracking.
Find whether a word exists as a path through adjacent cells. No cell can be reused. Mark the current cell visited before recursing, unmark it when you return.
This is where candidates leak points. The unmark step feels obvious, which means it happens automatically in your head and inconsistently in your code. Mark and unmark explicitly every single time, no exceptions.
This is the definitive grid backtracking problem. The mark-and-unmark pattern is the same one used in permutations, combinations, and path enumeration. Get it right here via the backtracking algorithm and it transfers everywhere.
9. Unique Paths (LeetCode 62)
Difficulty: Medium. Pattern: Grid DP.
A robot moves from top-left to bottom-right, only right or down. Count the distinct paths. Recurrence: dp[i][j] = dp[i-1][j] + dp[i][j-1]. Fill the table top-to-bottom, left-to-right.
This recurrence is the foundation of every 2D DP grid problem. Everything else in this section builds on it. Understand it now, not after it shows up in an interview and you're improvising.
10. Minimum Path Sum (LeetCode 64)
Difficulty: Medium. Pattern: Grid DP with costs.
Same structure as Unique Paths, but each cell has a cost. dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1]). Amazon, Google, and Meta all ask variants of this.
The standard follow-up is constant-space DP: write values back into the input grid. See the dynamic programming framework if the recurrence setup is unfamiliar.
11. Set Matrix Zeroes (LeetCode 73)
Difficulty: Medium. Pattern: In-place flagging.
If any cell is 0, zero its entire row and column. The O(1)-space solution uses the first row and first column as flags. Scan the body of the matrix first, update rows and columns, handle the first row and column last.
"Can you do it in constant space?" comes up in nearly every real interview where this appears. Know the trick before you walk in, or you'll spend ten minutes reinventing it under pressure while an interviewer watches.
12. Game of Life (LeetCode 289)
Difficulty: Medium. Pattern: In-place simultaneous-state update.
Conway's Game of Life on a matrix. Every cell updates simultaneously. Overwriting cells as you go means later reads see new values instead of old ones.
The fix: encode two states in one integer. A cell that was alive but will die becomes 2. Dead but will live becomes -1. Read the sign for the old state, the absolute value for the new state. It looks completely obvious in a solution walkthrough and completely non-obvious when the clock is ticking.
This encoding pattern applies anywhere a grid must capture a state transition without extra space.
13. Rotate Image (LeetCode 48)
Difficulty: Medium. Pattern: Transpose and reverse.
Rotate an n x n matrix 90 degrees clockwise, in-place. Transpose along the main diagonal, then reverse each row. Two O(n²) passes, zero extra space.
A rotation decomposes into two simpler operations. That decomposition instinct shows up in matrix problems well beyond this one. Practice it until it's immediate.
14. Spiral Matrix (LeetCode 54)
Difficulty: Medium. Pattern: Shrinking boundary traversal.
Collect all elements in spiral order. Maintain four pointers (top, bottom, left, right) and shrink each boundary after finishing that side. Stop when boundaries cross.
The shrink condition is where candidates lose it under pressure. Frequently asked at Amazon and Microsoft. Practice until boundary management is automatic, because you won't have bandwidth for it mid-interview when you're also trying to narrate and your IDE is open.
15. Search a 2D Matrix (LeetCode 74)
Difficulty: Medium. Pattern: Binary search on a sorted matrix.
Each row is sorted. The first element of each row is greater than the last of the previous row. Search for a target in O(log(mn)).
Map midpoint index to row and column: row = mid // n, col = mid % n. Then run standard binary search.
The problem tests whether you can see past the 2D structure to the 1D search beneath it. Which is the recurring theme of this entire list, honestly.
How to Practice These Without Wasting Time
Do them in order. The DFS problems build on each other. So do the DP and in-place problems. Jumping to Pacific Atlantic before Surrounded Regions, or Minimum Path Sum before Unique Paths, makes both harder than they need to be.
After your first pass, revisit the backtracking problems without notes. The undo step is easy to remember in theory and easy to skip under pressure, when your brain is occupied with a dozen other things and the interviewer is watching you think in silence.
Grid problems require more verbal explanation than most problem types. Before writing a line of code, you should be able to say which of the five families you're working with and why. Interviewers at Google and Amazon specifically score whether you articulate your approach before typing. Most candidates skip this step when they're nervous, which is exactly when it matters most. SpaceComplexity puts you in a live mock interview where that expectation is the default, which is exactly the habit grid problems demand.
Five Things Worth Knowing
- Multi-source BFS (problems 3 and 4) is underrepresented in most prep. Learn it before your interview, not during it.
- Boundary DFS (problems 5 and 6) always works backwards from the answer. When you see "cells that reach the border," think reverse DFS.
- The in-place tricks (problems 11, 12, 13) are almost always a follow-up in real interviews. Know them cold.
- Grid DP follows the same shape every time: fill left-to-right, top-to-bottom, using values already computed.
- If you're stuck on a grid problem, draw the graph explicitly. The structure usually reveals the pattern.