Reported September 2026
Goldman Sachstwo pointers

Trapping Rain Water

Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Goldman Sachs OA. Under 2s to a working solution.
Founder's read

Strip away the elevation-map story and Trapping Rain Water is one question asked at every index: how high is the water sitting on this bar? Goldman Sachs candidates reported this one in September 2026, and it's the classic version with a twist in the types. Heights go up to 10^9 and the array up to 10^6, so the answer needs a long. If you've seen the problem, you know the shape. If you haven't, the pattern is two pointers over the array. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment, but the idea is short enough to hold in your head.

The problem

You are given an array heights, where heights[i] is the height of the ith bar in an elevation map. Every bar has width 1.
Return the total amount of rainwater that can be trapped between the bars.

Function
trapRainWater(heights: long[]) → long

Examples
Example 1
heights = [3,0,0,2,0,4]
return = 10
The bars trap 3 + 3 + 1 + 3 = 10 units of water at indices 1, 2, 3, and 4.
Example 2
heights = [8,1,8,2,4]
return = 9
The first basin traps 7 units above the bar of height 1, and the last basin traps 2 units above the bar of height 2.

Constraints
0 <= heights.length <= 10^6
0 <= heights[i] <= 10^9
The answer fits in a signed long.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Water above bar i equals min(maxLeft, maxRight) minus heights[i], floored at zero. That's the whole problem. The brute force recomputes both maxes per index and dies at 10^6 elements, since it's O(n^2). Fix one: precompute prefix and suffix max arrays, O(n) time and O(n) space. Fix two: two pointers, left and right, tracking leftMax and rightMax. Move the pointer on the smaller side, because that side's water level is already decided by its own max. Pitfalls: accumulate in a long, not an int, since the sum can blow past 2^31. Handle length 0 and 1 by returning 0. Don't subtract before checking the max update. If you freeze on the pointer logic during the live OA, StealthCoder can hand you the working version, but write the prefix-max approach first if you're shaky.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as trapping rain water. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Goldman Sachs's OA.

Goldman Sachs reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Trapping Rain Water FAQ

What's the trick to Trapping Rain Water?+

Water over a bar is min(tallest bar to the left, tallest bar to the right) minus the bar's height. Everything else is just computing those two maxes efficiently. Prefix and suffix arrays work, and two pointers do it in constant extra space.

How hard is this one really for the Goldman Sachs OA?+

It's a well-known hard-tagged problem, but the solution is short once you see the min-of-maxes idea. The prefix/suffix version is easy to code correctly. Two pointers is the upgrade. Reported September 2026, so expect it or a close variant.

Why does the problem use long instead of int?+

Heights reach 10^9 and there can be 10^6 bars, so total water can exceed 32-bit range. Use a 64-bit accumulator from the start. An int overflow here gives wrong answers only on big hidden tests, which is the nasty kind of bug.

What edge cases should I test?+

Empty array returns 0. One or two bars return 0. A strictly increasing or decreasing array returns 0. All equal heights return 0. Also test example 2, [8,1,8,2,4], which should give 9, to confirm your basin handling.

How do I prepare for this in 48 hours?+

Code the prefix/suffix max version from scratch twice, then convert it to two pointers. Trace example 1 by hand and confirm you get 10. Time yourself on a 10^6 input in your head: it must be linear, no nested loops.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Goldman Sachs.

OA at Goldman Sachs?
Invisible during screen share
Get it