Trapping Rain Water
Updated
Problem statement
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[]) → intExamples
Example 1
height = [0,1,0,2,1,0,1,3,2,1,2,1]return = 6The bars trap 1 + 1 + 2 + 1 + 1 = 6 units of water.
Example 2
height = [4,2,0,3,2,5]return = 9The 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.
More Bloomberg LP Phone Screen questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Trapping Rain WaterTeradata · ONSITE INTERVIEW
- Trapping Rain WaterAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Trapping Rain WaterKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Trapping Rain WaterNuro · ONSITE INTERVIEWNuro interview questions
- Trapping Rain WaterMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Trapping Rain WaterRupeek · ONSITE INTERVIEW
- Rain Kept Between WallsDocuSign · PHONE SCREENDocuSign interview questions
- Rain Water Held Between BarsAdobe · PHONE SCREENAdobe interview questions
- Trapping Rain WaterGoogle · OAGoogle interview questions
- Count Non-Overlapping Pairs of Rectangles That Sum to KWisdomAI · PHONE SCREEN
Interview reports that mention this question
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg SWE Phone Screen and Onsite: 1 Experience
- Bloomberg New Grad Question Bank: 161 Questions (Part 1 of 2)
- Bloomberg New Grad VOs: 32 Experiences (Part 5 of 5)
- Bloomberg New Grad Screens: 30 Experiences (Part 4 of 5)