FastPrepSet Matrix Zeroes
Problems/Set Matrix Zeroes

Set Matrix Zeroes

Microsoft logoMicrosoft● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a review of a Microsoft software engineering internship. The review lists the Set Matrix Zeroes task by name only, with no input or rules. The round was the second technical round, after an online assessment and a first round with a substring question.

Here the matrix has m rows and n columns (each 1 to 100) of integers from -10^9 to 10^9. Every row and every column that holds a 0 in the original matrix becomes all 0s. Zeros written along the way do not spread further, and you return the resulting matrix. Doing it in place with constant extra space is the editorial's second approach. The tests do not check memory use.

Our version is about 95% the same as the reported question.

You are given an m x n integer matrix. For every cell that holds 0 in the original matrix, set every cell in its row and every cell in its column to 0.

Only the zeros of the original matrix count: zeros you write do not clear further rows or columns. Return the resulting matrix.

Function

setZeroes(matrix: int[][]) → int[][]

Examples

Example 1

matrix = [[4,2,7],[3,0,5],[8,6,9]]return = [[4,0,7],[0,0,0],[8,0,9]]
matrix
012042713052869
→
return
012040710002809
Moved or new

The only 0 is at row 1, column 1 (0-based). Row 1 and column 1 become all 0s; every other cell keeps its value.

Example 2

matrix = [[0,3,5,1],[2,4,6,0],[7,8,9,2]]return = [[0,0,0,0],[0,0,0,0],[0,8,9,0]]
matrix
0123003511246027892
→
return
0123000001000020890
Moved or new

The original zeros are at (0, 0) and (1, 3). So rows 0 and 1 and columns 0 and 3 become 0. In row 2 only the cells at columns 1 and 2 keep their values, 8 and 9.

Constraints

  • m == matrix.length, n == matrix[i].length, and 1 <= m, n <= 100
  • -10^9 <= matrix[i][j] <= 10^9

More Microsoft Onsite Interview questions

  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
  • Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Sudoku Board ValidationConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
  • Validate Rectangular Game PiecesGoogle · ONSITE INTERVIEWGoogle interview questions
  • Query Whether an Island Size ExistsMeta · ONSITE INTERVIEWMeta interview questions
  • Validate a Sudoku BoardZipRecruiter · ONSITE INTERVIEWZipRecruiter interview questions
  • Check a Partly Filled Sudoku GridExpedia · ONSITE INTERVIEWExpedia interview questions
  • Cache Shared Image Pipeline PrefixesAnthropic · ONSITE INTERVIEWAnthropic interview questions
  • Sparse Matrix MultiplicationByteDance · PHONE SCREENByteDance interview questions
  • First Completed Mingo LineEpic · OAEpic interview questions
  • Valid SudokuVerkada · PHONE SCREENVerkada interview questions
  • Count 2x2 Submatrices by Black CellsTikTok · OATikTok interview questions

Interview reports that mention this question

  • Microsoft SWE Intern Interviews: 244 Experiences (Part 1 of 6)

Microsoft interview experiences

  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
  • Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int[][] setZeroes(int[][] matrix) {
        // Write your code here.
    }
}
Checking account…