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.
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample Answer
Requirements
- Input:
gridof'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. ...