FastPrepRain Water Held Between Bars
Problems/Rain Water Held Between Bars

Rain Water Held Between Bars

Adobe logoAdobe● HardFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem asks how much rain water stays on top of a row of bars. Two Adobe reports put a trapped-water question in a first round. One names it only by a link. The other calls its question a variant without saying what changed, and mostly describes how the round went (several versions coded, a hand trace, extra edge cases asked).

Our choices: the reading where every dip between bars fills (the linked task), not the variant where only two bars hold water; at most 3000 bars with heights from 0 to 10^5; new examples.

The judged task matches the reports at about 80%.

A row of bars stands side by side; bar i has width 1 and height height[i]. Rain falls on the whole row and fills every dip until water would spill over a side. Water spills off both ends of the row.

Return how many units of water stay trapped on top of the bars.

Function

trappedWater(height: int[]) → int

Examples

Example 1

height = [4,1,3,0,2,5]return = 10

The two outer bars (4 and 5) hold the water at level 4 everywhere between them. Above the bars 1, 3, 0 and 2 that gives 3 + 1 + 4 + 2 = 10 units.

Example 2

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

The heights only rise, so every bar has nothing taller on its left: no water is held.

Example 3

height = [3,0,3,0,1]return = 4

Between the two 3s the water rises to 3 (3 units). The last pit is held only by 3 on the left and 1 on the right, so its level is 1 (1 unit). Total 4.

Constraints

  • 1 <= height.length <= 3000
  • 0 <= height[i] <= 10^5

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public int trappedWater(int[] height) {
    // Write your code here.
}
Checking account…