Trapping Rain Water
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure behind this one is just an array and two pointers, but Bloomberg reportedly put Trapping Rain Water in front of candidates in November 2025, and plenty of people still freeze on it. You get an array of bar heights and need the total water trapped between them. It's a classic, so the interviewer expects clean code fast. If you've seen it, it's five minutes. If you haven't, the logic feels slippery under a timer. StealthCoder sits invisibly on your screen during the live OA, so if your mind goes blank on the pointer logic, you have a safety net.
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
Water at index i equals min(leftMax, rightMax) minus height[i], floored at zero. The easy version builds two arrays: prefix max from the left and suffix max from the right, then sums the differences. That's O(n) time and O(n) space, and it's fully acceptable for n up to 2 * 10^4. The tighter version uses two pointers, left and right, plus running leftMax and rightMax. Move whichever side has the smaller current height, because that side's water level is already decided by its own max. Common pitfall: moving the wrong pointer, or forgetting that equal heights trap nothing. Also watch for a negative depth when a bar is the new max. Update the max first, then add water only when the bar is below it. If you blank mid-OA, StealthCoder reads the problem on screen and hands you the two-pointer solution as a hedge.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
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 Bloomberg's OA.
Bloomberg reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Trapping Rain Water FAQ
How hard is Trapping Rain Water really?+
It's rated hard on LeetCode, but the idea is short. Once you see that water depends on the smaller of the two side maxes, the code is about ten lines. The difficulty is spotting that insight cold, not writing it.
What's the trick to solving it fast?+
Think per bar. Each bar holds min(tallest left, tallest right) minus its own height. Precompute both maxes in two passes, or use two pointers moving inward from the shorter side. Either gets full credit on these constraints.
Do I need the two-pointer version or is the prefix array fine?+
With n up to 2 * 10^4, the prefix and suffix max arrays pass easily. Two pointers saves memory and impresses, but correctness matters more. Write the array version first if you're nervous, then optimize if time remains.
Is this still being asked by Bloomberg?+
It was reported in November 2025, so yes, it's current. Bloomberg has a reputation for array and string classics, so expect similar problems like this one rather than exotic ones.
How do I prepare in 48 hours?+
Trace example 2 by hand: [4,2,0,3,2,5] gives 9. Then code the prefix max version from memory, then the two-pointer version. Test edge cases like a single bar, all equal heights, and strictly increasing heights, which all return 0.