Accenture · Question 10 · Arrays
Set Matrix Zeroes
Problem
If an element is 0, set its entire row and column to 0 in - place.
Approach
Use first row and column as markers.
Time O(m×n)Space O(1)
How it works
- 1Mark zero rows/columns on the first row and column.
- 2Zero the inner cells from the markers.
- 3Fix the first row and column last.
Sample input / output
Input: matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1]] Output: [[1, 0, 1], [0, 0, 0], [1, 0, 1]]
Constraints
1 <= m, n <= 200; O(1) extra space.
Solution — read it, then walk it
Time O(m×n)Space O(1)Loops 10Branches 8Returns 1source · C++
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/37
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits