Zero out every row and column that contains a zero, in place

Quick Overview

Given an integer matrix, set every row and every column that contains a zero to all zeros, changing the matrix in place. It tests two-pass matrix updates, keeping new zeros from spreading, and reducing extra memory to a constant.

Zero out every row and column that contains a zero, in place

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an `m x n` integer matrix, if any element is 0, set its entire row and its entire column to 0. Do this in place. ### Function Signature ```python def set_zeroes(matrix: list[list[int]]) -> None: ``` ### Rules - Modify `matrix` in place and return nothing. The result is the contents of `matrix` after the call. - Only zeros present in the input trigger a row and column reset. A cell that becomes 0 during the update does not clear its own row or column. ### Constraints - `m` is the number of rows and `n` is the number of columns. - `1 <= m, n <= 250` - `-10^4 <= matrix[i][j] <= 10^4` ### Examples ```text Input: matrix = [[2,3,4],[5,0,6],[7,8,9]] Output: [[2,0,4],[0,0,0],[7,0,9]] ``` The only zero is at row 1, column 1, so row 1 and column 1 become all zeros and the four corner values stay. ```text Input: matrix = [[0,5,7,1],[2,4,6,8],[3,9,0,2]] Output: [[0,0,0,0],[0,4,0,8],[0,0,0,0]] ``` The zeros at (0, 0) and (2, 2) clear rows 0 and 2 and columns 0 and 2. In row 1, only the values in columns 1 and 3 survive. ```text Input: matrix = [[1,2],[0,4],[5,6]] Output: [[0,2],[0,0],[0,6]] ``` The zero at (1, 0) clears row 1 and column 0. The 2 and the 6 sit outside both, so they stay.

Overview: Given an integer matrix, set every row and every column that contains a zero to all zeros, changing the matrix in place. It tests two-pass matrix updates, keeping new zeros from spreading, and reducing extra memory to a constant.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an m x n integer matrix, if any element is 0, set its entire row and its entire column to 0. Do this in place.

Function Signature

def set_zeroes(matrix: list[list[int]]) -> None:

Rules

  • Modify matrix in place and return nothing. The result is the contents of matrix after the call.
  • Only zeros present in the input trigger a row and column reset. A cell that becomes 0 during the update does not clear its own row or column.

Constraints

  • m is the number of rows and n is the number of columns.
  • 1 <= m, n <= 250
  • -10^4 <= matrix[i][j] <= 10^4

Examples

Input: matrix = [[2,3,4],[5,0,6],[7,8,9]]
Output: [[2,0,4],[0,0,0],[7,0,9]]

The only zero is at row 1, column 1, so row 1 and column 1 become all zeros and the four corner values stay.

Input: matrix = [[0,5,7,1],[2,4,6,8],[3,9,0,2]]
Output: [[0,0,0,0],[0,4,0,8],[0,0,0,0]]

The zeros at (0, 0) and (2, 2) clear rows 0 and 2 and columns 0 and 2. In row 1, only the values in columns 1 and 3 survive.

Input: matrix = [[1,2],[0,4],[5,6]]
Output: [[0,2],[0,0],[0,6]]

The zero at (1, 0) clears row 1 and column 0. The 2 and the 6 sit outside both, so they stay.