Trapping Rain Water
Reported by candidates from Zolostays's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Zolostays asked this in September 2024, and it's a classic that trips up candidates who haven't seen it. You're given an elevation map and need to calculate how much water gets trapped after it rains. The naive greedy approach fails because you need to know the maximum height to your left and right at every position. This is a two-pointer or dynamic-programming problem depending on which direction you approach it. StealthCoder can feed you the pattern instantly if you blank on the setup.
Pattern and pitfall
The core trick: water trapped at any position equals the minimum of the max height to its left and the max height to its right, minus the ground height at that position. Most candidates try to iterate once and calculate on the fly, which doesn't work because you don't have future context. The winning move is either precompute left and right max arrays in two passes (DP/prefix approach), or use two pointers converging inward while tracking the running max on both sides. The two-pointer version is tighter in code and space. Watch for off-by-one errors at the boundaries. If you freeze during the OA, StealthCoder gives you both solutions instantly so you pick the one that feels natural to code.
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 Zolostays's OA.
Zolostays 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
Is this actually a medium or hard problem?+
Medium-hard. The logic is straightforward once you see it, but the setup is not obvious without practice. You need to think about constraints, not just simulate. Most candidates solve it in 20-25 minutes if they've drilled two-pointer or prefix-max patterns.
Can I solve this with a greedy approach?+
No. Pure greedy fails because you can't decide how much water fits at a position without knowing what's ahead. You must precompute or use pointers that know context. That's where most blanks happen.
What's the time and space trade-off?+
Two-pass DP: O(n) time, O(n) space. Two-pointer: O(n) time, O(1) space. Both are optimal. Pick whichever feels more natural to you. The two-pointer version is slightly faster to code under pressure.
Will Zolostays ask follow-ups after I solve it?+
Possibly. Be ready to explain why greedy fails, why you chose DP or two-pointers, and how to optimize space. If they ask a variant (e.g., with obstacles), the core logic stays the same but you adjust the constraint logic.
How do I prep this in 24 hours?+
Code the two-pointer version once by hand. Trace through a small example like [0,1,0,2,1,0,1,3,2,1,2,1]. Get the intuition that you're squeezing from both ends, tracking the bottleneck. One solid run-through beats five half-runs.