Water Trapped Between Bars of a Server Rack Elevation Map

Last updated: November 29, 2026

Quick Overview

Given non-negative integer heights of adjacent unit-width bars, compute how many units of rainwater are trapped between them after it rains.

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

0

0

0 solved


Given non-negative integer heights of adjacent unit-width bars, compute how many units of rainwater are trapped between them after it rains.

Trapping rain water is a long-standing Meta onsite problem used to see whether a candidate can move from an O(n) space prefix-max solution to the O(1) space two-pointer solution, and justify why the two-pointer step is safe.

What the Interviewer Expects
  • States water at i is min(max left, max right) minus height
  • Presents prefix and suffix max arrays first
  • Optimizes to two pointers with O(1) space
  • Explains why the shorter side can be settled
  • Handles short arrays and monotonic inputs
Key Topics to Cover
Two pointers
Prefix max
Monotonic stack
Arrays
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
  • Solve the 2D version on a height grid.
  • Solve it with a monotonic stack, filling water layer by layer.
  • Heights stream in left to right; what can you report online?
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

Heights up to 2 * 10^4 elements, each between 0 and 10^5. Output total trapped units. Target O(n) time and O(1) extra space....

High-Level Design

Water above bar i equals min(maxLeft[i], maxRight[i]) - h[i]. Prefix and suffix arrays give O(n) space. To drop them, move pointers from both ends while tracking left_max and right_max. If `left...


Submit Your Answer
Markdown supported