Clear Every Row and Column That Contains a Damaged Bin

Last updated: November 29, 2026

Quick Overview

In a grid of storage bins, a zero marks a damaged bin. Set every cell in the same row or column as any damaged bin to zero, in place, using O(1) extra space.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


In a grid of storage bins, a zero marks a damaged bin. Set every cell in the same row or column as any damaged bin to zero, in place, using O(1) extra space.

Warehouse safety rules lock entire aisles and levels when one bin fails inspection. The interviewer checks that you avoid cascading zeros and can use the first row and column as marker storage with special handling for them.

What the Interviewer Expects
  • Avoid zeroing while scanning, which cascades incorrectly
  • Start with an O(m + n) solution using row and column flags
  • Reduce to O(1) space by storing markers in the first row and column
  • Track separately whether the first row or column must be cleared
  • Clear the interior before the first row and column
Key Topics to Cover
matrix manipulation
in-place markers
space optimization
two pass algorithms
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Why must the first row and column be processed last?
  • How would you do this for a sparse grid stored as a list of damaged coordinates?
  • How would you parallelize it on a very large grid?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: an m x n list of integers. Output: nothing; mutate the grid so any row or column containing an original zero becomes all zeros. Zeros created during the process must not spread further....

High-Level Design

First record whether row 0 and column 0 originally contain a zero. Then scan the interior cells: for a zero at (i, j), set grid[i][0] = 0 and grid[0][j] = 0 as markers. Second pass over the interi...


Submit Your Answer
Markdown supported