Water Trapped on a 3D Terrain Elevation Map

Last updated: November 29, 2026

Quick Overview

Given a 2D grid of terrain heights, return how much rain water the terrain can hold after a storm, where water drains off the grid edges.

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

0

0

0 solved


Given a 2D grid of terrain heights, return how much rain water the terrain can hold after a storm, where water drains off the grid edges.

A well known Google onsite hard question, here framed around Maps terrain data. The interviewer wants the boundary min-heap flood fill and a clear explanation of why the lowest wall on the frontier determines the water level of its neighbors.

What the Interviewer Expects
  • Seeds a min-heap with all boundary cells
  • Processes the lowest frontier cell first
  • Tracks the current water level as the max seen on the path
  • Adds water where a neighbor is below the level
  • States O(mn log(mn))
Key Topics to Cover
Min-Heap
BFS
Grid
Priority Flood
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
  • Why does the 1D two-pointer method not generalize directly?
  • What if some cells are drains (holes)?
  • Return the height of water at each cell
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: m x n grid of non-negative heights.
  • Water escapes off the boundary.
  • Output: total trapped water volume (unit cells)....
High-Level Design

Water in a cell can rise only to the lowest point on the best escape route to the edge. Think of the boundary as a wall. The lowest wall segment is the weak point: any unvisited neighbor of it can hol...


Submit Your Answer
Markdown supported