Set Matrix Zeroes
Updated
Problem statement
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]]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]]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, and1 <= 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 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)