Trapping Rain Water
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reportedly put Trapping Rain Water in front of candidates in September 2026, and the whole solution hinges on one choice: what you keep track of as you scan the array. Two pointers closing in from both ends is the clean answer. A monotonic stack also works, and so does precomputed prefix and suffix max arrays. If you've got an OA invite and 48 hours, learn the two-pointer version cold, because it runs in O(n) time and O(1) space. If you blank mid-assessment, StealthCoder is the invisible safety net that reads the problem and hands you the approach.
The problem
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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that water at index i depends only on the shorter of the two tallest walls around it. So you don't need both maxes for every index at once. Put a left pointer and a right pointer at the ends, and track leftMax and rightMax. Whichever side has the smaller current height is the bottleneck, so process that side: if height[left] < height[right], update leftMax or add leftMax - height[left], then move left. Otherwise mirror it on the right. The common pitfall is adding water before updating the max, or comparing maxes instead of current heights, which gives wrong totals on valleys like [4,2,0,3,2,5]. Also remember bars of height 0 and arrays shorter than 3 trap nothing. With n up to 2 * 10^4, an O(n^2) brute force might squeak by, but don't bet the OA on it. If your brain freezes live, StealthCoder is the hedge that surfaces the two-pointer solution.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Trapping Rain Water cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as trapping rain water. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Trapping Rain Water FAQ
What's the trick to Trapping Rain Water?+
Water above a bar is set by the shorter of the tallest walls on either side. Use two pointers from both ends with running leftMax and rightMax. Always advance the pointer on the lower side, since that side's max is the true bottleneck. It's one pass, constant extra space.
How hard is this one really for the Amazon OA?+
It's a classic hard-tagged problem, but the pattern is well known. Once you see the min of left and right max formula, the code is about ten lines. The difficulty is getting the pointer logic right under pressure, not inventing anything new.
Can I use prefix and suffix max arrays instead?+
Yes. Build leftMax and rightMax arrays, then sum max(0, min(leftMax[i], rightMax[i]) - height[i]). It's O(n) time but O(n) space. It's easier to get right first try, so it's a fine fallback if the two-pointer version feels shaky.
What edge cases should I test?+
Test a single bar, two bars, a strictly increasing array, a strictly decreasing array, and all zeros. All of these should return 0. Then run both examples: [0,1,0,2,1,0,1,3,2,1,2,1] should give 6 and [4,2,0,3,2,5] should give 9.
How do I prepare in 48 hours?+
Write the two-pointer solution from memory three times, then the stack version once. Trace example 2 by hand to see why the lower side moves. Skip broad grinding. Knowing this one pattern plus its edge cases covers the likely follow-ups.