Clear Every Row and Column Touching an Error Cell In Place

Last updated: November 29, 2026

Quick Overview

Given an m x n grid of numeric cells where 0 marks a cell in error, set the entire row and column of every original 0 to 0, modifying the grid in place with O(1) extra space.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given an m x n grid of numeric cells where 0 marks a cell in error, set the entire row and column of every original 0 to 0, modifying the grid in place with O(1) extra space.

A Google technical screen favorite because the O(m + n) answer is easy and the O(1) answer requires a trick. The interviewer watches whether you avoid cascading zeros (new zeros spreading further) and whether you can reuse the first row and column as marker storage safely.

What the Interviewer Expects
  • Explains why zeroing while scanning cascades incorrectly
  • Gives the O(m + n) marker-array solution first
  • Reuses the first row and column as markers for O(1) space
  • Tracks whether the first row and first column themselves need clearing
  • Processes the marker row and column last
Key Topics to Cover
In-Place Mutation
Marker Encoding
Matrix Traversal
Space Optimization
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
  • What if the grid is stored row by row on disk and only two rows fit in memory?
  • Instead of zeroing, mark affected cells with a sentinel without losing the original values.
  • Parallelize this across many workers holding horizontal stripes of the 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: grid, m x n ints, mutated in place.
  • Every row and column that contained a 0 in the original grid becomes all zeros.
  • Extra space must be O(1)....
High-Level Design

Naively zeroing as you scan creates new zeros that then trigger more clearing. The fix is to record first and act later. With O(m + n) space, two boolean arrays do the job.

For O(1), store the flags ...


Submit Your Answer
Markdown supported