Trapping Rain Water
Updated
Problem statement
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[]) → longExamples
Example 1
heights = [3,0,0,2,0,4]return = 10The 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 = 9The 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^60 <= heights[i] <= 10^9- The answer fits in a signed
long.
More InMobi Onsite Interview questions
- Russian Doll EnvelopesONSITE INTERVIEW · Seen Jul 2026
- Minimum and Maximum with Optimal ComparisonsONSITE INTERVIEW · Seen Dec 2025
- Minimum Edges to Connect All ComponentsONSITE INTERVIEW · Seen Dec 2025
- Shortest Path Visiting All NodesONSITE INTERVIEW · Seen Sep 2025
- Maximum Sum of Non-Adjacent Tree NodesONSITE INTERVIEW · Seen Jan 2022
- Word SquaresONSITE INTERVIEW · Seen Jan 2022
- Maximum Length Pair ChainONSITE INTERVIEW · Seen Jul 2021
- Top View of a Binary TreeONSITE INTERVIEW · Seen Jul 2021
- Maximum XOR Between Two ArraysONSITE INTERVIEW · Seen May 2021
- Validate Binary Search TreeONSITE INTERVIEW · Seen May 2021
Similar questions at other companies
- Count Hotspot Playback WindowsGoogle · ONSITE INTERVIEWGoogle interview questions
- Subarray with a Given SumExpedia · ONSITE INTERVIEWExpedia interview questions
- Trapping Rain WaterGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Count Non-Overlapping Pairs of Rectangles That Sum to KWisdomAI · PHONE SCREEN
- Running Through Walls with Limited EnergyScale AI · OAScale AI interview questions
- Eating CandiesTikTok · OATikTok interview questions
- Get Minimum Round Trip CostAmazon · OA
- Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Run-Length Encoded Vector Dot ProductMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Subarray Sum Equals KServiceNow · ONSITE INTERVIEWServiceNow interview questions