Set Matrix Zeros Problem Statement

Given an N x M integer matrix, if an element is 0, set its entire row and column to 0's, and return the matrix. Specifically, if a cell has a value 0 (i.e., matrix[i][j] == 0), change all cells in the ith row and jth column to 0. Perform modifications in-place.

Input:

                The first line of the input contains an integer T representing the number of test cases.                    For each test case, the first line contains two space-separated integers N and M, denoting the number of rows and columns of the matrix.                       For the next N lines, each line contains M space-separated integers representing the matrix elements.                           

Output:

            For each test case, print the modified matrix. Print the output of each test case in a separate line.           

Example:

Input:
1
2 3
7 19 3
4 21 0
Output:
7 19 0
0 0 0

Constraints:

  • 1 ≤ T ≤ 1000
  • 1 ≤ M, N ≤ 1000
  • Σ(M * N) ≤ 2000000
  • -231 ≤ matrix[i][j] ≤ 231-1, for all 1 ≤ i ≤ N and 1 ≤ j ≤ M.
  • Time Limit: 1 sec

Note:

You are not required to print anything; the function should return the modified matrix as the output.

Follow-up:

Can we optimize space usage better than O(M * N)? Using O(M + N) space improves memory use, but further optimizations can be made to use constant space. Can you achieve this?
AnswerBot
4mo

To solve the Set Matrix Zeros problem, we can use O(1) space by utilizing the first row and column to store information about zeros in the rest of the matrix.

  • Iterate through the matrix and use the fir...read more

Saurabh Kumar
2y
currently not working
 rows,cols = len(matrix),len(matrix[0]) arr_rows,arr_cols = [False]*rows,[False]*cols for i in range(rows): for j in range(cols): if matrix[i][j] == 0: arr_rows[i] = True arr_cols[j] = True for i in r...read more
Saurabh Kumar
2y
currently not working

rows,cols = len(matrix),len(matrix[0]) arr_rows,arr_cols = [False]*rows,[False]*cols for i in range(rows): for j in range(cols): if matrix[i][j] == 0: arr_rows[i] = True arr_cols[j] = True for i in r...read more

Select
Add answer anonymously...

ServiceNow Software Engineer interview questions & answers

A Software Engineer was asked 9mo agoQ. How would you design a system similar to Google Drive?
A Software Engineer was asked 11mo agoQ. Write an algorithm to perform topological sort on a directed acyclic graph.
A Software Engineer was asked Q. Write an algorithm for the Tower of Hanoi problem.

Popular interview questions of Software Engineer

A Software Engineer was asked 9mo agoQ1. How would you design a system similar to Google Drive?
A Software Engineer was asked 11mo agoQ2. Describe the low-level design of a hashmap.
A Software Engineer was asked Q3. Write an algorithm for the Tower of Hanoi problem.
ServiceNow Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits