Set Matrix Zeroes
A medium Matrix interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Matrix
- Sheets
- 2
- Core for
- 10 roles
The problem
Given an m-by-n matrix, modify it in place so that if any cell contains zero, its entire row and column are set to zero.
Example 1
- Input
- matrix = [[4,7,2],[5,0,9],[3,6,1]]
- Output
- [[4,0,2],[0,0,0],[3,0,1]]
- Why
- The zero at row 1, column 1 wipes out its whole row and its whole column.
Example 2
- Input
- matrix = [[3,0,5,8],[2,7,6,4],[0,9,1,5]]
- Output
- [[0,0,0,0],[0,0,6,4],[0,0,0,0]]
Constraints
- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 200
- -2^31 <= matrix[i][j] <= 2^31 - 1
How to think about it
Updated 2026-09-29The matrix can serve as its own marker table. The first row and first column already exist and can signal whether a row or column needs zeroing. The only complication is that cell (0,0) belongs to both, needing one extra boolean for the column.
Approaches, worst first
Extra marker arrays
time O(m*n) · space O(m+n)
Use two boolean arrays, one per row and one per column, to record which need zeroing. Scan the matrix once to fill them, then apply in a second pass, spending O(m+n) extra memory on state the matrix could track itself.
First row and column as markersWrite this one
time O(m*n) · space O(1)
Use matrix[i][0] and matrix[0][j] as the flags for whether row i or column j should be zeroed. A separate boolean tracks column 0 since matrix[0][0] is shared by both row 0 and column 0. The matrix marks first, then zeros in a second pass.
Where people lose marks · 3
- Updating matrix cells during the first pass can destroy a marker you have not yet read. You must record all zeros first, then apply the markings, or the information is lost mid-scan.
- The cell at (0,0) serves both row 0 and column 0. If only matrix[0][0] marks column zeroing, there is no way to distinguish row 0 from column 0 when both should be zeroed. A separate boolean for the column resolves this.
- Zeroing a row or column before all markers are read corrupts the remaining markers. The marking and zeroing phases must be strictly separated.
The theory behind it
Matrix — the ground this problem stands on. All Matrix problems
What Matrix is
A matrix is a flat grid of cells laid out in rows and columns, like a checkerboard or a spreadsheet. Each cell has an address made of two numbers: its row index going down and its column index going across. Because computer memory stores these rows one after another or as arrays of arrays, jumping straight to any individual cell takes the same tiny fraction of time no matter where it sits on the board.
When to reach for it
Reach for matrix techniques when the input is a two-dimensional grid, board, or map. Common prompts include walking through a maze, finding connected islands of land in water, rotating an image ninety degrees in place, or reading numbers in a spiral. Words like rows, columns, neighbors, adjacent squares, or diagonal lines are clear signs. It also appears in grid planning where each cell depends on the values above and to the left.
How the pattern works
Think of navigating the grid using directional coordinate offsets. Define row and column step arrays for moving up, down, left, and right. Always check that new row and column indices stay strictly between zero and the board boundaries before reading cell values. For spiral scans, track four boundary lines for top, bottom, left, and right, shrinking them inward after reading each side. When searching connected regions, mark visited cells directly or write a marker value into the cell to avoid looping.
More Matrix problems
Target Roles
Core requirement for 10 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .