Goldman Sachs interview question
Compute how much rainwater is trapped between bars of given heights
- Company:
- Goldman Sachs
- Type:
- Coding
- Difficulty:
- Hard
- Reported:
- October 25, 2026
Problem
You are given a list of non-negative integers heights, where each value is the height of a bar of width 1 and the bars stand next to each other in order. After it rains, water collects in the dips between bars. Return the total volume of water that is trapped.
Example 1
Input: heights = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Water fills the dips at indices 2, 4, 5, 6 and 9 for a total of 6 units.
Example 2
Input: heights = [4,2,0,3,2,5]
Output: 9
The bars of height 4 and 5 bound a basin that holds 2 + 4 + 1 + 2 = 9 units.
Constraints
- Every height is a non-negative integer.
Reported by candidates or drawn from publicly available sources, and rewritten in our own words. Pathwise Prep does not publish confidential assessment content. Report a concern