Skip to main content

32. Trapping Rain Water

hardAsked at Lyft

Compute water trapped between elevation bars — Lyft uses the two-pointer scan pattern to calculate residual ride capacity between demand spikes across time windows, finding how much latent supply is 'trapped' between peak surge events.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

Constraints

  • n == height.length
  • 1 <= n <= 2 * 10^4
  • 0 <= height[i] <= 10^5

Examples

Example 1

Input
height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output
6

Explanation: The elevation map traps 6 units of rain water between the bars.

Example 2

Input
height = [4,2,0,3,2,5]
Output
9

Approaches

1. Precomputed max arrays

For each index i, water trapped = min(maxLeft[i], maxRight[i]) - height[i]. Precompute maxLeft (running max from left) and maxRight (running max from right) in two passes. Third pass sums water.

Time
O(n)
Space
O(n)
function trap(height) {
  const n = height.length;
  const maxLeft = new Array(n).fill(0);
  const maxRight = new Array(n).fill(0);

  maxLeft[0] = height[0];
  for (let i = 1; i < n; i++) maxLeft[i] = Math.max(maxLeft[i - 1], height[i]);

  maxRight[n - 1] = height[n - 1];
  for (let i = n - 2; i >= 0; i--) maxRight[i] = Math.max(maxRight[i + 1], height[i]);

  let water = 0;
  for (let i = 0; i < n; i++) water += Math.min(maxLeft[i], maxRight[i]) - height[i];
  return water;
}

Tradeoff:

2. Two pointers (O(1) space)

Use left and right pointers. Move the pointer with the smaller max inward, accumulating water as min(leftMax, rightMax) - height[ptr]. The key insight: water at i is determined by the shorter of the two boundary walls.

Time
O(n)
Space
O(1)
function trap(height) {
  let left = 0, right = height.length - 1;
  let leftMax = 0, rightMax = 0;
  let water = 0;

  while (left < right) {
    if (height[left] < height[right]) {
      if (height[left] >= leftMax) leftMax = height[left];
      else water += leftMax - height[left];
      left++;
    } else {
      if (height[right] >= rightMax) rightMax = height[right];
      else water += rightMax - height[right];
      right--;
    }
  }
  return water;
}

Tradeoff:

Lyft-specific tips

Trapping Rain Water is a Lyft hard that tests whether you can articulate the two-pointer invariant under pressure: 'If leftMax < rightMax, we know the left bar's water is bounded by leftMax regardless of what's on the right — so we safely process the left side.' Say this out loud before coding. Start with the precomputed-array solution to prove correctness, then optimize to two pointers. Lyft engineers respect the iterative refinement process as much as the final answer.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Trapping Rain Water and other Lyft interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →