Skip to main content
All Questions

Set Matrix Zeroes

Medium
Unlock detailed company stats for this questionUpgrade
Given a matrix of size M x N, set all rows and columns to zeroes where an element is zero, using constant space complexity.

Given an m x n integer matrix, whenever an element is 0, set its entire row and column to 0. Modify matrix in place. The function doesn't return anything.

Example 1

Input: matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1]] Output: [[1, 0, 1], [0, 0, 0], [1, 0, 1]]

Example 2

Input: matrix = [[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]]

The two zeros in the first row clear the first row and the first and last columns.

Constraints

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

Related questions

01 Matrix

Related courses

Course

Data Engineering Interview Prep

Land your dream data engineering role at Meta, Google, Amazon, Microsoft, Walmart, DoorDash, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Practice data modeling, pipeline design, SQL, coding, behavioral interviews, and more.

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.