Total Floodwater Trapped on a City Elevation Grid

Last updated: November 29, 2026

Quick Overview

Given an R x C grid of street elevation heights, rain fills low areas until water would spill off the grid edge. Return the total volume of water trapped, which a routing team uses to flag streets likely to flood during storms.

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

0

0

0 solved


Given an R x C grid of street elevation heights, rain fills low areas until water would spill off the grid edge. Return the total volume of water trapped, which a routing team uses to flag streets likely to flood during storms.

A Hard Uber onsite grid problem with a routing motivation (avoiding flooded streets during storms). The interviewer wants the boundary-inward min-heap approach, where the lowest wall on the current frontier determines the water level, and a clear argument for why.

What the Interviewer Expects
  • Explains why the 1D two-pointer approach does not generalize
  • Seeds a min-heap with all border cells
  • Pops the lowest frontier cell and fills neighbors up to its level
  • Pushes neighbors with height max(own, level)
  • States O(RC log(RC))
Key Topics to Cover
Heaps
Matrix
BFS
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
  • How would you return which cells hold water deeper than a threshold?
  • What if rain only falls on part of the grid?
  • How would you update the answer when one cell's elevation changes?
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: list of lists of non-negative ints. Output: total trapped water as an int. Water escapes through any border cell, and flows 4-directionally....

High-Level Design

Water at a cell rises to the lowest point of the tallest barrier separating it from the edge. Start with all border cells in a min-heap (they cannot hold water). Repeatedly pop the lowest frontier cel...


Submit Your Answer
Markdown supported