Grid and Matrix Problems in Coding Interviews: A Practical Playbook
3 views
Grid problems are a favorite in coding interviews because they combine graph traversal, careful indexing, and edge cases in a format that is easy to explain on a whiteboard. "Number of islands", "rotting oranges", "shortest path in a binary matrix", "spiral order", and "rotate image" all belong to this family. Once you see the patterns, most of them become routine.
Treat the grid as a graph
Each cell is a node, and its neighbors (usually up, down, left, right, sometimes diagonals) are edges. Define a directions list once at the top, for example [(1,0),(-1,0),(0,1),(0,-1)], and write a small in-bounds check. That removes most indexing bugs and makes the code easy to read.
Pattern 1: Flood fill with DFS or BFS
"Number of islands" asks how many connected groups of land cells exist. Loop over every cell; when you find unvisited land, increment the count and run DFS or BFS to mark the whole island visited. You can mark visited by changing the grid value in place, but ask the interviewer whether mutating input is acceptable; if not, use a separate visited set. Complexity is O(rows x cols) time.
Mention the recursion-depth risk: a large grid that is all land can overflow the call stack with recursive DFS in some languages. An iterative stack or BFS avoids that, and pointing it out is a good signal.
Pattern 2: BFS for shortest paths
When all moves cost the same, BFS finds the shortest path. Push the start cell with distance zero, expand level by level, and stop when you reach the target. For "shortest path in a binary matrix" with eight directions, the only change is the directions list.
Pattern 3: Multi-source BFS
"Rotting oranges" and "walls and gates" start from many cells at once. Push all sources into the queue first, then run a single BFS. Each level represents one minute or one step. This is much faster than running a BFS from every source separately, and explaining why is worth a sentence.
Pattern 4: Dynamic programming on grids
"Unique paths", "minimum path sum", and "maximal square" use a DP table where each cell depends on its top and left neighbors. Write the recurrence first, handle the first row and column as base cases, then fill row by row. Mention the space optimization of keeping only one row when the recurrence allows it.
Pattern 5: In-place transformations
"Rotate image by 90 degrees" can be done by transposing the matrix and then reversing each row. "Set matrix zeroes" can use the first row and column as markers to achieve O(1) extra space. "Spiral order" uses four boundaries (top, bottom, left, right) that shrink after each pass. Walk through a 3x3 and a 3x4 example, since rectangular matrices catch many off-by-one errors.
Pattern 6: Grids with weights or obstacles
If moving between cells has different costs, BFS is no longer enough. Use Dijkstra with a priority queue, or 0-1 BFS with a deque when costs are only zero or one. For problems like "shortest path with K obstacle eliminations", the state becomes (row, col, remaining_eliminations), and you should say that the visited set must include the extra dimension.
Edge cases to check
- Empty grid or a grid with one row or one column.
- Start or target blocked.
- All cells the same value.
- Very large grids, where recursion depth and memory matter.
How to communicate
Say which pattern you see, state the complexity, define the directions and bounds check, then code. Dry-run on a tiny grid while showing the queue or stack contents. That narration is often what interviewers remember.
Practice approach
Solve two problems per pattern and then mix them randomly. For live rounds, Linkjob AI (https://linkjob.ai) provides real-time help during job interviews and technical interviews, and some candidates use it to keep their approach structured when they feel stuck. With the patterns above, grid questions turn into some of the most predictable problems you can face.