Water Pooled on a 2D Height Map of a Data Center Roof

Last updated: November 29, 2026

Quick Overview

Given a 2D grid of roof heights, return how many units of rainwater stay pooled after rain, where water can drain off any edge cell.

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

0

0

0 solved


Given a 2D grid of roof heights, return how many units of rainwater stay pooled after rain, where water can drain off any edge cell.

A hard Meta onsite question that extends the familiar 1D trapping problem to two dimensions, framed as data center roof drainage. The interviewer is looking for the boundary-inward priority-queue sweep and a convincing reason why the lowest boundary cell decides each neighbor's water level.

What the Interviewer Expects
  • Explain why the 1D two-pointer method does not extend directly
  • Seed a min-heap with all boundary cells
  • Always expand from the lowest boundary cell
  • Raise each newly visited cell's effective height to the current level
  • Analyze O(mn log(mn))
Key Topics to Cover
priority queue
Dijkstra-like flood fill
BFS
grid
boundary sweep
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
  • What if some interior cells are drains?
  • How would you report the volume per separate pool?
  • Can you do it with a bucket queue when heights are small integers?
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 by n grid of non-negative heights.
  • Water escapes through any edge cell.
  • Output: total trapped units (sum over cells of water depth)....
High-Level Design

Water above a cell can rise only to the lowest point on the wall surrounding it, where the wall is the path to the edge with the smallest maximum height.

Sweep inward from the edge, like Dijkstra...


Submit Your Answer
Markdown supported