Set Matrix Zeroes
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google reported this one in September 2026, and the detail that matters is the O(1) auxiliary space rule. Zero out the row and column of every original zero, in place, and return the same matrix. The hinted pattern says breadth-first search, but this is really a matrix problem with a marker trick. There's also a discussion-only follow-up about matrices too big for memory, so expect to talk after you code. If you've got an OA invite and 48 hours, learn the first-row-and-column marker approach cold. StealthCoder is the safety net on the live OA if your mind goes blank mid-problem.
The problem
You are given a nonempty rectangular integer matrix matrix. If an element is 0, set every element in its row and column to 0. Modify the matrix in place using O(1) auxiliary space, then return the same transformed matrix for comparison. Only zeroes present in the original matrix determine which rows and columns become zero. Interview follow-up Be prepared to explain how to process a matrix too large to fit in memory by scanning storage-backed blocks. This external-memory variant is discussion-only; the judged function receives the in-memory matrix described above. Function setZeroes(matrix: int[][]) → int[][] Examples Example 1 matrix = [[1,1,1],[1,0,1],[1,1,1]] return = [[1,0,1],[0,0,0],[1,0,1]] The original zero at row 1, column 1 makes the middle row and middle column zero. Example 2 matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]] return = [[0,0,0,0],[0,4,5,0],[0,3,1,0]] The original zeroes in the first row require that row and the first and fourth columns to become zero. Constraints 1 <= matrix.length <= 200. 1 <= matrix[0].length <= 200. Every row has the same number of columns. -2^31 <= matrix[i][j] <= 2^31 - 1. The algorithm must use O(1) auxiliary space for the judged in-memory matrix.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to use the matrix's own first row and first column as the memory you aren't allowed to allocate. First, record in two booleans whether row 0 and column 0 originally contain a zero. Then scan the inner cells, and whenever matrix[i][j] is 0, set matrix[i][0] and matrix[0][j] to 0. Next, scan the inner cells again and zero any cell whose row marker or column marker is 0. Last, use the two booleans to zero row 0 and column 0 if needed. The common pitfall is zeroing as you go. That turns new zeroes into fake originals and wipes the whole grid. Another is handling the first row and column before the inner cells, which destroys your markers. BFS isn't needed here. For the external-memory follow-up, say you'd scan blocks twice, keeping row and column zero flags. If you blank during the live OA, StealthCoder can surface the full solution.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Set Matrix Zeroes 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as set matrix zeroes. 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Set Matrix Zeroes FAQ
What's the trick for Set Matrix Zeroes in O(1) space?+
Use the first row and first column as flag storage. Save two booleans for whether row 0 and column 0 had zeroes originally. Mark flags from the inner cells, apply them in a second pass, then fix row 0 and column 0 last using the booleans.
How hard is this really for the Google OA?+
Medium. The idea is short, but the order of operations is easy to botch. An O(m+n) extra-space version is simple, so the real test is dropping to O(1) without corrupting your markers. Practice the three-pass flow until it's automatic.
Why not just zero cells as I find a zero?+
Newly written zeroes look identical to original ones. You'd then zero rows and columns that shouldn't change, and the whole matrix could end up zero. Only original zeroes count, so record first and apply later.
How do I answer the too-large-for-memory follow-up?+
It's discussion only. Scan the storage in blocks and keep a row-flag array and a column-flag array. One pass collects which rows and columns contain zeroes, and a second pass rewrites each block. Mention the I/O cost of two full scans.
How should I prepare in 48 hours?+
Code the marker approach from scratch twice, then test the edge cases: a single row, a single column, a zero in the corner, and a zero only in the first row. Know the space tradeoff from O(m+n) to O(1) well enough to explain it.