Top 150 InterviewEasy

Set Matrix Zeroes

Detailed guide and Python implementation for the 'Set Matrix Zeroes' problem.

Problem Statement

Easy

Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.

You must do it in place.

Implement a function setZeroes(matrix: list) -> list that modifies the matrix in place and returns it.

Constraints
  • m == matrix.length
  • n == matrix[0].length
  • 1 <= m, n <= 200
  • -2^31 <= matrix[i][j] <= 2^31 - 1

Examples

Example 1
Input
[[1,1,1],[1,0,1],[1,1,1]]
Output
[[1,0,1],[0,0,0],[1,0,1]]
Explanation

The element at position (1,1) is 0. So the entire row 1 and column 1 are set to 0.

Example 2
Input
[[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]]
Explanation

Elements at (0,0) and (0,3) are 0. Row 0 becomes all zeros. Columns 0 and 3 become all zeros.

Need a Hint?
Consider using Math & Geometry-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.