FastPrepTrapping Rain Water
Problems/Trapping Rain Water

Trapping Rain Water

AMD logoAMD● HardFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names Rainwater Trapping. The standard1D/unit-width interpretation, numeric bounds, examples and callable interface are authored; approximate source match90%.

Nonnegative bar heights describe a one-dimensional elevation map with unit-width bars. Return the total units of rainwater retained between the bars after rain stops. Water can escape from either end; arrays with fewer than three bars retain no water.

Function

trappedWater(height: int[]) → int

Examples

Example 1

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

The retained depths at indices 2, 4, 5, 6 and 9 are 1, 1, 2, 1 and 1, totaling 6.

Example 2

height = [3,0,2,0,4]return = 7

Interior depths are 3, 1 and 3, totaling 7.

Constraints

  • 0 <= height.length <= 1000.
  • 0 <= height[i] <= 1000.

More AMD problems

  • Insert After a Linked-List NodeONSITE INTERVIEW · Seen Oct 2026
  • LRU CacheONSITE INTERVIEW · Seen Oct 2026
  • Lowest Common Ancestor of a Binary TreePHONE SCREEN · Seen Oct 2026
All AMD interview questionsSee AMD hiring insights
Saved
public int trappedWater(int[] height) {
    // Write your code here.
}
Checking account…