Back to Practice
#0293

Set Matrix Zeroes

MediumDSA28 min20 XP

Problem

Given a matrix of integers, return a new matrix where any row or column containing a 0 is set entirely to 0.

Why This Matters

This matrix marker problem is a common interview check for separating detection from mutation.

Function Signature

def set_zeroes(matrix):

Examples

Example 1
Inputmatrix = [[1,1,1],[1,0,1],[1,1,1]]
Output[[1, 0, 1], [0, 0, 0], [1, 0, 1]]

The center zero clears its whole row and column.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Center zero
Input[[1,1,1],[1,0,1],[1,1,1]]
Expected[[1,0,1],[0,0,0],[1,0,1]]

Row 1 and column 1 become zero.

Multiple zeroes
Input[[0,1,2],[3,4,5],[6,0,8]]
Expected[[0,0,0],[0,0,5],[0,0,0]]

Rows 0 and 2, columns 0 and 1 are cleared.

Hidden Test Categories
no zeroesall zeroessingle row