Trapping Rain Water
Reported by candidates from Kotak Mahindra Bank's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Kotak Mahindra Bank reportedly put Trapping Rain Water in front of candidates in September 2026, and the array can reach 20,000 elements. That size is the whole point. A per-index scan for the tallest bar on each side is O(n^2), and it's the first thing most people write. This is a two-pointers or prefix-max problem in disguise. If you've got an invite for this one, you need the linear approach ready before you open the editor. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment, but the trick is short enough to own tonight.
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 depth at index i is min(leftMax, rightMax) minus height[i], floored at zero. Brute force recomputes both maxes for every index, so n = 20,000 means roughly 400 million operations. Fix it one of two ways. Precompute leftMax and rightMax arrays in two passes, then sum the depths: O(n) time, O(n) space. Or use two pointers with running maxes. Move the pointer on the lower side, because that side's max is the binding limit, and add max minus height there. That gives O(1) space. The common pitfall is moving the wrong pointer or forgetting the floor at zero. Also check n = 1 and n = 2, which trap nothing. Sums stay small enough for a standard int here, but use a wide type if you're nervous. If you freeze during the live OA, StealthCoder can surface the two-pointer solution while you keep typing.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
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. If you're reading this with an OA window open, you're who this was built for.
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 Kotak Mahindra Bank's OA.
Kotak Mahindra Bank reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Trapping Rain Water FAQ
What's the trick for Trapping Rain Water?+
Water above a bar is set by the shorter of the tallest bars on either side. Track left max and right max, and always process the side with the smaller current max. That makes the answer one pass with two pointers instead of rescanning for every index.
Will brute force pass with n up to 20,000?+
Probably not. Scanning left and right for every index is O(n^2), around 400 million steps at the upper bound. Some tests may squeak by, but large cases risk timing out. Use prefix and suffix max arrays or two pointers so you don't gamble.
Prefix arrays or two pointers, which should I write?+
Prefix and suffix arrays are easier to get right under pressure and still O(n). Two pointers saves memory and looks cleaner. If you can explain the pointer-movement rule, use it. Otherwise write the arrays and pass the tests.
What edge cases break solutions here?+
Arrays of length 1 or 2 trap zero water. Strictly increasing or decreasing arrays also trap nothing. Flat arrays give zero as well. Make sure each index's contribution is floored at zero, and that the pointer loop ends when the pointers meet.
How do I prepare for this in 48 hours?+
Write the prefix-max version from scratch, then rewrite it with two pointers. Trace Example 2 by hand and confirm you get 9. Then test a decreasing array and a single element. Two clean runs of that routine is enough for this problem.