Water Trapped on a Minecraft Heightmap After Rainfall
Last updated: November 29, 2026
Quick Overview
A Minecraft terrain heightmap gives the column height of every block position in a rectangular area. After heavy rain, water pools wherever it cannot drain to the edge of the area; return the total volume of trapped water.
Microsoft
November 29, 20260
0
0 solved
A Minecraft terrain heightmap gives the column height of every block position in a rectangular area. After heavy rain, water pools wherever it cannot drain to the edge of the area; return the total volume of trapped water.
The two dimensional version of trapping rain water appears in Microsoft onsites as a hard follow-up to the one dimensional problem. The interviewer expects you to explain why the 1D two pointer trick fails and to use a min-heap flood from the boundary.
What the Interviewer Expects
- Explain why the 1D left max and right max approach does not extend to 2D
- Seed a min-heap with all boundary cells
- Always expand from the lowest wall cell
- Fill a neighbor up to the current water level and push its effective height
- Reach O(R * C * log(R * C))
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
- How would you return the water level per cell, not just the total?
- What changes if some cells are drains that remove all water?
- How would you update the answer after a single block is mined?
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: matrix of non-negative integer heights.
- Water drains off the outer boundary.
- Output: total trapped water units.
- Grids with fewer than 3 rows or columns trap nothing....
High-Level Design
Water at a cell rises to the lowest barrier on any path to the boundary. The boundary is the initial wall. Put all boundary cells into a min-heap keyed by height.
Repeatedly pop the lowest wall cell....