Set Matrix Zeroes
Set entire row and column to 0 if element is 0 Input: m x n matrix Output: modified matrix (in-place and return)
Set Matrix Zeroes is a classic scan-and-record problem: the right bookkeeping beats brute force. Interviewers love it because it reveals your data-structure instincts and your edge-case discipline. Watch your edge cases around repeats, and be clear about what your map is storing.
Examples
You write
set_zeroes(matrix: List[List[int]])
Example 1
- Input
- matrix = [[1,1,1],[1,0,1],[1,1,1]]
- returns
- [[1,0,1],[0,0,0],[1,0,1]]
Example 2
- Input
- matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
- returns
- [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.