Coding interview problems › Two Pointers
↔️ Two Pointers Hard
Trapping Rain Water
Trapping Rain Water explained: water above each bar is min(left max, right max) minus its height. Two pointers solve it in O(n) time, O(1) space. Tested Python.
Also searched as: trapping rainwater problem · rain water trapping two pointers
Try it live Free
Two Pointers
Watch two pointers close in from both ends, always moving the side whose limit is already known. Trapping Rain Water uses that rule, adding up water bar by bar as the pointers move.
Also useful: Stacks & the Monotonic Stack
The problem
Bars of width 1 stand side by side with non-negative heights. After it rains, water collects in the dips between taller bars. Compute the total units of water trapped.
The key insight
The water above bar i is min(tallest bar to its left, tallest bar to its right) - height[i]. With two pointers you never need both maxima exactly: whichever side has the smaller running max is the side whose water level is already decided, because the other side is guaranteed to have a wall at least that tall.
Step-by-step approach
- Set
i = 0,j = n - 1, and running maximalmax = rmax = 0. - While
i < j: ifheight[i] < height[j], the left side is the limiting one. - On the left: update
lmax, addlmax - height[i]to the answer, moveiright. - Otherwise do the mirror image on the right with
rmaxand movejleft. - Return the total.
Python solution
def trap(height):
i, j = 0, len(height) - 1
lmax = rmax = water = 0
while i < j:
if height[i] < height[j]: # left wall is the limit
lmax = max(lmax, height[i])
water += lmax - height[i]
i += 1
else: # right wall is the limit
rmax = max(rmax, height[j])
water += rmax - height[j]
j -= 1
return waterComplexity
Each bar is processed once as one pointer moves inward. Only two running maxima and the total are stored.
Common mistakes
- Using only the left maximum, which overfills dips at the right edge where no tall right wall exists.
- Updating the running max after adding water, which can add a negative amount for a new tallest bar.
- Treating it like Container With Most Water and only looking at two lines.
Frequently asked questions
What is the time and space complexity of Trapping Rain Water?
The two-pointer solution is O(n) time and O(1) space. The prefix and suffix max array solution is O(n) time and O(n) space, and the monotonic stack solution is also O(n).
Why can we decide the water at the lower side?
If height[i] < height[j], there is a wall of at least height[j] on the right, so bar i's level is set by the left maximum alone. The unknown parts of the right side cannot lower it.
How does the monotonic stack solution work?
Keep indices of decreasing heights. When a taller bar arrives, pop the bottom of a dip and add water bounded by the new bar and the bar now on top of the stack, layer by layer.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Two Pointers Two Pointers lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.