Reported November 2020
SambaNova Systemsmatrix

Set Matrix Zeroes

Reported by candidates from SambaNova Systems's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live SambaNova Systems OA. Under 2s to a working solution.
Founder's read

SambaNova Systems reportedly put Set Matrix Zeroes in front of candidates in November 2020. The grid is up to 200 x 200, so a naive approach that zeroes as it scans will wreck your own data and wipe the whole matrix. The ask is in-place with constant extra space, and that's where people stall. The hinted pattern here is BFS, but it's really a matrix marker trick. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you the solution as a safety net. Know the trick first and you probably won't need it.

The problem

Given an m x n integer matrix matrix, if an element is 0, set every element in its row and column to 0.
Modify the matrix in place using constant extra space, then return the transformed matrix.

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

Examples
Example 1
matrix = [[1,1,1],[1,0,1],[1,1,1]]
return = [[1,0,1],[0,0,0],[1,0,1]]
The zero at row 1, column 1 makes the entire middle row and middle column zero.
Example 2
matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
return = [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
The zeroes in the first row make the first and fourth columns zero, and the first row is already required to become all zeroes.

Constraints
m == matrix.length.
n == matrix[0].length.
1 <= m, n <= 200.
-2^31 <= matrix[i][j] <= 2^31 - 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to use the first row and first column as your marker storage instead of allocating extra arrays. First, record whether the first row and the first column originally contain a zero, using two booleans. Then scan the rest of the matrix, and when matrix[i][j] is 0, set matrix[i][0] and matrix[0][j] to 0. Next, scan the inner cells again and zero any cell whose row marker or column marker is 0. Last, zero the first row and first column only if their booleans say so. The common pitfall is zeroing immediately during the scan, which turns original nonzero cells into fake zeros and cascades across the grid. The second pitfall is forgetting the first row and column flags, so the markers get confused with real zeros. BFS is a red herring. It's O(mn) time and O(1) space. If the ordering of those passes slips away during the live OA, StealthCoder is your hedge.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Set Matrix Zeroes cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as set matrix zeroes. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass SambaNova Systems's OA.

SambaNova Systems reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Set Matrix Zeroes FAQ

What's the trick for Set Matrix Zeroes?+

Store the row and column markers inside the matrix itself, in the first row and first column. Save two booleans first for whether that row and column had a zero originally. Mark, then apply to the inner cells, then handle the first row and column last.

Is BFS actually needed here?+

No. The BFS hint is misleading. Nothing spreads or propagates level by level. You mark rows and columns once and apply them in a second pass. A BFS approach would add queue memory and break the constant space requirement.

Why can't I just zero cells while scanning?+

Because you'd create new zeros that weren't in the original input. Those fake zeros would then trigger more row and column wipes later in the scan. You must separate detecting zeros from applying them, which is why the marker pass comes first.

Is an O(m + n) space solution acceptable?+

The problem states constant extra space, so the O(1) marker approach is what's asked for. Two boolean arrays are a fine first draft and easy to get right. Then convert them into first row and first column markers to meet the constraint.

How do I prepare for this in 48 hours?+

Code the O(1) version from scratch three times on blank paper. Test it on a matrix with a zero in the first row, and another with a zero in the first column. Those two edge cases are where most failed submissions break.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with SambaNova Systems.

OA at SambaNova Systems?
Invisible during screen share
Get it