Best Empty Cell for a Blast Clearing the Most Targets

Last updated: November 29, 2026

Quick Overview

Given a grid of walls 'W', targets 'T' and empty cells '0', a blast placed on an empty cell clears every target in its row and column until it hits a wall; return the most targets one blast can clear.

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

0

0

0 solved


Given a grid of walls 'W', targets 'T' and empty cells '0', a blast placed on an empty cell clears every target in its row and column until it hits a wall; return the most targets one blast can clear.

A Google onsite grid problem where the brute force is O(mn(m + n)). The interviewer is looking for the observation that row and column counts only change at walls, so they can be cached per segment and reused.

What the Interviewer Expects
  • States the brute force cost and why it is too slow for large grids
  • Caches the row-segment target count and recomputes only after a wall
  • Keeps one cached column count per column
  • Achieves O(mn) total time
  • Considers walls at the edges and grids with no empty cells
Key Topics to Cover
Matrix Traversal
Segment Caching
Dynamic Programming
Amortized Analysis
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
  • Allow placing the blast on a target cell too.
  • Return all best positions, not only the count.
  • What if the blast range is limited to k cells in each direction?
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 of 'W', 'T', '0'.
  • A blast on '0' clears targets in all four directions until a wall or the edge.
  • Output: maximum targets cleared, 0 if no empty cell....
High-Level Design

For a cell, the targets it clears equal the targets in its row segment (between walls) plus those in its column segment. These counts are the same for every cell in the segment, so compute them once. ...


Submit Your Answer
Markdown supported