Fill Matrix
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google's Fill Matrix question hit candidates in February 2024, and it's a BFS trap dressed as a grid problem. You've got a matrix, probably with some cells marked or unmarked, and you need to fill or flood it in a specific order. The instinct is to DFS it recursively, but Google wants to see BFS. If you blank on the traversal order during the live OA, StealthCoder will have the pattern locked in seconds so you don't waste 20 minutes second-guessing yourself.
Pattern and pitfall
Fill Matrix is a breadth-first-search problem because the fill propagates level by level, not depth by depth. You start from one or more source cells, add them to a queue, then expand outward. The key gotcha: candidates write DFS (call a recursive function), which works but hits stack limits on large matrices or gets the order wrong. BFS is cleaner, more predictable, and passes the large test cases. The pattern is essentially multi-source BFS, similar to 0-1 Matrix on LeetCode. You initialize a queue with all starting cells, mark them visited, then process neighbors level by level. Watch the edge cases: boundaries, already-filled cells, and whether the fill stops at certain obstacles. During the live assessment, if you second-guess the traversal method, StealthCoder gives you the queue-based scaffold so you stay on track.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Fill Matrix cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as 01 matrix. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Fill Matrix FAQ
Is this the same as '0-1 Matrix' or 'Rotting Oranges'?+
Pattern-wise, yes. All three are multi-source BFS where you start from multiple cells and expand outward level by level. The narrative changes, but the algorithm is identical. If you've solved either of those, you've solved Fill Matrix.
Why BFS and not DFS?+
BFS naturally gives you the correct 'distance' or 'fill order' when propagating from multiple sources. DFS works but is harder to reason about and can hit recursion limits on huge matrices. Google's test cases likely include large grids, so BFS is safer.
What are the common mistakes on this problem?+
Forgetting to mark cells as visited before adding to the queue (causes infinite loops). Not initializing the queue with all source cells. Off-by-one errors on boundary checks. Using a set instead of a visited matrix and losing performance. Start small, test on a 3x3 grid.
How much time do I spend on this in a real OA?+
If you recognize the BFS pattern immediately, 15-20 minutes to code and test. If you don't, you might waste 30+ minutes rewriting from DFS. That's why pattern recognition matters. Spend the first 2 minutes confirming: multiple sources, level-by-level expansion, queue.
Can I solve this without BFS?+
Technically DFS or even brute force works on small inputs, but it fails on Google's hidden test cases. BFS is the expected solution. If you're unsure during the OA, assume BFS is the pattern Google wants to see.