Grokking 75: Top Coding Interview Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Set Matrix Zeroes (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given a 2D grid of numbers called matrix, if any number in the grid is 0, set the entire row and column containing that zero to zeros.

The grid should be modified in place without using any extra grid.

Examples

Example 1:

  • Input: matrix =
[[2, 3, 4], 
 [5, 0, 6], 
 [7, 8, 9]]
  • Expected Output:
[[2, 0, 4], 
 [0, 0, 0], 
 [7, 0, 9]]
  • Justification: The element at position (1, 1) is zero. So, the second row and column are set to zero.

Example 2:

  • Input: matrix =
[[0, 2, 3], 
 [4, 5, 6], 
 [7, 8, 9]]
  • Expected Output:
[[0, 0, 0], 
 [0, 5, 6], 
 [0, 8, 9]]
  • Justification: The element at position (0, 0) is zero. So, the first row and column are set to zero.

Example 3:

  • Input: matrix =
[[1, 2, 3, 7], 
 [4, 0, 6, 8], 
 [0, 8, 9, 6],
 [1, 4, 6, 4]]
  • Expected Output:
[[0, 0, 3, 7], 
 [0, 0, 0, 0], 
 [0, 0, 0, 0],
 [0, 0, 6, 4]
  • Justification: The elements at position (1, 1), and (2, 0) are zero. So, the respected rows and columns are set to zero.

Constraints:

  • m == matrix.length
  • n == matrix[0].length
  • 1 <= m, n <= 200
  • -2<sup>31</sup> <= matrix[i][j] <= 2<sup>31</sup> - 1

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible