Back to Practice Dashboard
Top 150 InterviewEasy

Set Matrix Zeroes

Learn how to solve the 'Set Matrix Zeroes' problem. This detailed resource details brute force and optimized approaches.

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?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

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

Open in Editor