Trapping Rain Water
Given `n` non-negative integers representing an elevation map where the width of each bar is `1`, compute how much water it can trap after raining. **Examples:** ```typescript trap([0,1,0,2,1,0,1,3,2,1,2,1]) // 6 trap([4,2,0,3,2,5]) // 9 ```
Solve It in the BrowserPractice Explaining It Out Loud
Optimal Approach
- Time complexity: O(n)
- Space complexity: O(1)
Two-pointer technique converges in a single pass of length n.
Follow-Up Questions Interviewers Ask About Trapping Rain Water
- Why does two pointers work without precomputing max arrays?
Water trapped at an index depends only on min(leftMax, rightMax). If leftMax < rightMax, the bottleneck is guaranteed to be leftMax.
Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.