Set Matrix Zeroes
Bit Manipulation & Math
Asked at 12 companies
6 recent
- Bloomberg
- Meta
- Microsoft
- Amazon
- tcs
- Nykaa
- eBay
- ZScaler
- Nutanix
+2 moreShow fewer
- Walmart Labs
- Zoho
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · File permissions: Unix stores read, write and execute as three bits, so chmod 755 is just bits: 7 = 111 (rwx), 5 = 101 (r-x).
About this problem
XOR tricks, bit counting, and matrix / number puzzles.
Try it in the editor below for 20–30 minutes: passing its tests counts as solved and starts your reviews. The full statement is on LeetCode.
Examples
Full statement on LeetCode ↗Example 1
- Input
- matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]
- Output
- [[1, 0, 1], [0, 0, 0], [1, 0, 1]]
Walk through it step by step(6 steps · shows the approach, so try first)
- No extra memory: the first row and first column become the notebook. [r][0] = 0 will mean "zero row r", matrix[0][c] = 0 "zero column c". Column 0 can't note itself too, so one flag remembers whether it had a 0 of its own: false.
- (1, 1) is 0: write the note in its row's first cell (1, 0) and its column's top cell (0, 1).
- Row 2: a note says its row or column is zeroed, so (2, 1) become 0.
- Row 1: a note says its row or column is zeroed, so (1, 2) become 0.
- Row 0: no note covers these cells, so they stay.
- Every row and column that held a 0 is now all zeros, using no extra memory.
Example 2
Just one item- Input
- matrix = [[1, 0]]
- Output
- [[0, 0]]
Example 3
- Input
- matrix = [[0, 1, 2, 0],[3, 4, 5, 2],[1, 3, 1, 5]]
- Output
- [[0, 0, 0, 0],[0, 4, 5, 0],[0, 3, 1, 0]]
- The function changes
matrixin place; Output shows it afterwards.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: optimal solution
Solution code
Optimal solutionUse the first row and first column as the 'zero this' markers, with one extra flag for whether the first column itself must be zeroed. Mark in one pass, then fill zeros so markers are read before they're overwritten.
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
rows, cols = len(matrix), len(matrix[0])
first_col = any(matrix[r][0] == 0 for r in range(rows))
for r in range(rows):
for c in range(1, cols):
if matrix[r][c] == 0:
matrix[r][0] = matrix[0][c] = 0
for r in range(rows - 1, -1, -1):
for c in range(cols - 1, 0, -1):
if matrix[r][0] == 0 or matrix[0][c] == 0:
matrix[r][c] = 0
if first_col:
matrix[r][0] = 0Reference solution, tested in Python, Java and C++.
Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.