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 Goldman Sachs Phone Screen questions
- House Robber IIIPHONE SCREEN · Seen Nov 2026
- Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Check a Complete Binary TreePHONE SCREEN · Seen Oct 2026
- Meeting RoomsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Count Staircase WaysPHONE SCREEN · Seen Oct 2026
- Is a Power of TenPHONE SCREEN · Seen Oct 2026
- Minimum Traveling Salesperson TourPHONE SCREEN · Seen Oct 2026
- Square Root with Newton IterationPHONE SCREEN · Seen Oct 2026
- First Non-Repeating CharacterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Root of the Largest TreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Count Non-Overlapping Pairs of Rectangles That Sum to KWisdomAI · PHONE SCREEN
- Count Hotspot Playback WindowsGoogle · ONSITE INTERVIEWGoogle interview questions
- Subarray with a Given SumExpedia · ONSITE INTERVIEWExpedia interview questions
- Trapping Rain WaterInMobi · ONSITE INTERVIEWInMobi interview questions
- 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
- Interval List IntersectionsGrammarly · PHONE SCREEN