Count Map Tiles That Detach When Supports Are Knocked Out
Last updated: November 29, 2026
Quick Overview
A grid has tiles (1) and empty cells (0). A tile is stable if it is in the top row or 4-directionally connected to a stable tile. Hits remove tiles one at a time, and after each hit every unstable tile falls and disappears. Return how many tiles fall after each hit.
0
0
0 solved
A grid has tiles (1) and empty cells (0). A tile is stable if it is in the top row or 4-directionally connected to a stable tile. Hits remove tiles one at a time, and after each hit every unstable tile falls and disappears. Return how many tiles fall after each hit.
This hard onsite question is the classic example of reversing time to turn deletions into unions. Framed around map tile caching where a tile stays valid only if it links back to an anchored row, the interviewer checks the reverse process, a virtual ROOF node with sizes, and careful handling of hits on empty cells.
What the Interviewer Expects
- Explains why forward simulation is too slow
- Applies all hits first, then restores them in reverse
- Uses a virtual ROOF node and union by size
- Computes fallen tiles as the growth of the ROOF component minus one
- Handles hits on empty cells and repeated hits
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
- What if tiles can also be stable by touching the left wall?
- Return which tiles fell, not just how many.
- How would you parallelize the initial union pass on a huge grid?
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: grid of 0/1 and a list of
(r, c)hits. - A hit on an empty cell does nothing.
- The hit tile itself does not count as fallen.
- Output: list of fallen counts per hit....
High-Level Design
Forward simulation would need a full traversal after each hit. Instead, apply every hit first (decrementing so that hits on empty cells become -1), and build union-find on what remains, linking top-ro...