FastPrepTrapping Rain Water
Problems/Trapping Rain Water

Trapping Rain Water

Nuro logoNuro● HardFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source identifies LeetCode 42, Trapping Rain Water, but does not reproduce its callable interface, examples, or numeric bounds. The judged task follows the named exercise and is an approximately 99% match.

You are given an integer array height of length n. n vertical bars stand on the x-axis. The i-th bar has width 1 and height height[i].

Compute how many units of water the bars can trap after rain.

Water sits above a bar only when both a strictly taller left boundary and a strictly taller right boundary exist. The water depth at index i is max(0, min(leftMax[i], rightMax[i]) - height[i]), where leftMax[i] is the tallest bar at an index < i and rightMax[i] is the tallest bar at an index > i.

Function

trap(height: int[]) → int

Examples

Example 1

height = [0,1,0,2,1,0,1,3,2,1,2,1]return = 6

The bars trap 1 + 1 + 2 + 1 + 1 = 6 units of water.

Example 2

height = [4,2,0,3,2,5]return = 9

The valley between the height-4 and height-5 bars traps 2 + 4 + 1 + 2 = 9 units.

Constraints

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

More Nuro problems

  • Merge Timestamped Record ListsPHONE SCREEN · Seen Oct 2026
  • Assign Robots to Their Nearest Available ItemsPHONE SCREEN · Seen Oct 2026
  • Container With Most WaterPHONE SCREEN · Seen Oct 2026
All Nuro interview questionsSee Nuro hiring insights
Saved
class Solution {
    public int trap(int[] height) {
      // Write your code here.
    }
}
Checking account…