Your zero-rows-and-columns code wipes the whole matrix. Why?
Question
Wherever the input grid has a 0, set that cell's whole row and whole column to 0. Change the grid in place.
For this grid, row 0 and column 2 should become 0, and every other cell should keep its value:
input expected 5 8 0 3 0 0 0 0 2 7 4 6 2 7 0 6 9 1 5 4 9 1 0 4
The code below sets every cell to 0 instead. Find the bug and fix it.
def set_zeroes(grid): rows, cols = len(grid), len(grid[0]) for r in range(rows): for c in range(cols): if grid[r][c] == 0: for j in range(cols): grid[r][j] = 0 for i in range(rows): grid[i][c] = 0 grid = [[5, 8, 0, 3], [2, 7, 4, 6], [9, 1, 5, 4]] set_zeroes(grid) print(grid) # every cell is 0
Show hints (3)
- "WHICH ZEROS COUNT"
The rule talks about zeros in the input. After the first 0, the loop moves on to
grid[0][3]and finds a 0 there too. Was that 0 in the input? - "READ, THEN WRITE"
Split the work into two passes. The first pass only reads and notes which rows and columns had a 0. The second pass writes.
- "TWO SETS"
In the first pass, add
rtozero_rowsandctozero_colsfor every 0. In the second pass, set a cell to 0 whenr in zero_rows or c in zero_cols.
Show checked explanation
The loop writes zeros while it is still looking for zeros. It then finds the zeros it wrote and treats them as input zeros, so they spread. Use two passes: first record which rows and columns had a 0, then write. That is O(m·n) time and O(m + n) extra memory for an m × n grid.
def set_zeroes(grid): zero_rows, zero_cols = set(), set() for r, row in enumerate(grid): for c, value in enumerate(row): if value == 0: zero_rows.add(r) zero_cols.add(c) for r, row in enumerate(grid): for c in range(len(row)): if r in zero_rows or c in zero_cols: row[c] = 0 grid = [[5, 8, 0, 3], [2, 7, 4, 6], [9, 1, 5, 4]] set_zeroes(grid) print(grid)
[[0, 0, 0, 0], [2, 7, 0, 6], [9, 1, 0, 4]]
Code run on CPython 3.12 against the examples above. Checked by an editor other than the author.
Walkthrough
Follow the buggy loop. It reads the grid row by row, and every time it sees a 0 it wipes a row and a column.
| cell | in the input? | what the loop wipes |
|---|---|---|
| (0, 2) | yes | row 0 and column 2 |
| (0, 3) | no, written at (0, 2) | column 3 |
| (1, 2) | no, written at (0, 2) | row 1 |
| (1, 3) | no, written at (0, 3) | nothing new |
| (2, 2) | no, written at (0, 2) | row 2 |
| (2, 3) | no, written at (0, 3) | nothing new |
Only one of these six zeros was in the input. After row 2 is wiped, every cell is 0.
The written zeros are results, not new instructions. But the loop can't tell them apart from input zeros, because it reads the same grid it writes. With this bug, any 0 in the top row wipes the whole grid. Its column becomes 0, so every row below has a 0, and the loop wipes each of those rows when it gets there.
The fix reads first and writes later. The first pass finds the input zeros at a moment when nothing has changed yet: here only row 0 and column 2. The second pass writes, but it decides from the two sets, not from the grid. So a new 0 can't cause more zeros.
Both passes visit every cell once, so the time is O(m·n). The sets hold at most m row numbers and n column numbers. A full copy of the grid would also work, but it costs O(m·n) memory.
Common mistakes
- Marking cells with a special value such as -1, then turning the marks into zeros at the end. If the grid can hold -1, a real -1 is wiped too.
[[-1, 2], [3, 0]]becomes all zeros. - In the O(1)-memory version, where the first row holds the column marks, zeroing the first row before the inner cells. Every column then looks marked. The example's row 1 becomes
[2, 0, 0, 0]. - Calling the function twice. The second call treats the new zeros as input.
[[2, 0], [5, 3]]becomes[[0, 0], [5, 0]], and a second call makes it all zeros. - Testing with a grid built as
[[1] * 3] * 3. All three rows are the same list, so writing one row changes all of them, even when the function is right.
Follow-up questions
- "Can you do it with O(1) extra memory?" Use the first row and column as the notes:
grid[r][0] = 0marks rowr,grid[0][c] = 0marks columnc. First save two flags: did row 0 or column 0 have a 0? Then mark from the inner cells, zero the inner cells from the marks, and apply the two flags last. - "Zeros are rare, and you already get their positions as a list." Then skip the scan. Collect the rows and columns from the list and write only those. Each marked row costs n writes and each marked column m writes, so you never touch the other cells.
What a strong answer covers
Names the cause: the loop reads zeros it wrote itself. Separates reading from writing, gives the two-set fix with its cost, then the O(1)-memory version with two flags saved first. Tests a 0 in the first row and in the first column.
Open in interactive practice deck →