Calculate Trapping Rain Water - Problem

Given a table Heights containing id and height columns, calculate the amount of rainwater that can be trapped between the bars in the landscape. Each bar has a width of 1 unit.

Problem: For each position, water can be trapped if there are taller bars on both sides. The water level at any position is determined by the minimum of the maximum heights to its left and right, minus the current height.

Table Structure:

  • id: Sequential position identifier (primary key)
  • height: Height of the bar at that position

Return the total amount of trapped rainwater as a single value.

Table Schema

Heights
Column Name Type Description
id PK int Sequential position identifier (primary key)
height int Height of the bar at that position
Primary Key: id
Note: id values are guaranteed to be in sequential order representing positions in the landscape

Input & Output

Example 1 — Basic Rain Water Trapping
Input Table:
id height
1 0
2 1
3 0
4 2
5 1
6 0
7 1
8 3
Output:
trapped_water
5
💡 Note:

Water gets trapped at positions where there are higher bars on both sides. Position 3 (height=0) traps 1 unit between bars at positions 2 and 4. Position 5 (height=1) traps 1 unit, and position 6 (height=0) traps 1 unit. Additional water is trapped at other positions, totaling 6 units.

Example 2 — No Water Trapped
Input Table:
id height
1 3
2 2
3 1
4 0
Output:
trapped_water
0
💡 Note:

This is a decreasing sequence of heights, so no water can be trapped. Water would flow off to the right side, resulting in 0 trapped water.

Example 3 — Single Valley
Input Table:
id height
1 3
2 0
3 2
Output:
trapped_water
2
💡 Note:

Position 2 has height 0 and is surrounded by heights 3 and 2. The water level is min(3,2) = 2, so 2 units of water are trapped at position 2.

Constraints

  • 1 ≤ number of rows ≤ 20000
  • 0 ≤ height ≤ 35000
  • id values are sequential and start from 1

Visualization

Tap to expand
Trapping Rain Water INPUT 0 1 0 2 1 0 1 3 Heights Table id height 1,2,3,4... 0,1,0,2... ALGORITHM STEPS 1 Initialize Arrays Create leftMax[] and rightMax[] arrays 2 Left Pass Scan left-to-right Track max height seen 3 Right Pass Scan right-to-left Track max height seen 4 Calculate Water water[i] = min(leftMax, rightMax) - height[i] Formula per position: water = min(L_max, R_max) - height[i] Sum all positive values FINAL RESULT 2 1 2 1 Bar height Trapped water Total Water Trapped 6 units OK - Sum: 2+1+2+1 = 6 Key Insight: Water at position i depends on the minimum of maximum heights on both sides. Using two passes (left-to-right and right-to-left) achieves O(n) time complexity with O(n) space. Two-pointer technique can optimize to O(1) space by computing water level on-the-fly. TutorialsPoint - Calculate Trapping Rain Water | Optimal Solution
Asked in
Amazon 28 Microsoft 22 Google 19
28.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen