0
0
DSA Pythonprogramming~20 mins

Trapping Rain Water Using Stack in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stack Rain Water Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Trapping Rain Water Calculation
What is the output of the following Python code that calculates trapped rain water using a stack?
DSA Python
def trap(height):
    stack = []
    water = 0
    for i, h in enumerate(height):
        while stack and height[stack[-1]] < h:
            top = stack.pop()
            if not stack:
                break
            distance = i - stack[-1] - 1
            bounded_height = min(height[stack[-1]], h) - height[top]
            water += distance * bounded_height
        stack.append(i)
    return water

print(trap([0,1,0,2,1,0,1,3,2,1,2,1]))
A6
B5
C8
D7
Attempts:
2 left
💡 Hint
Think about how the stack helps find boundaries to trap water between bars.
🧠 Conceptual
intermediate
1:30remaining
Understanding Stack Usage in Trapping Rain Water
Why do we use a stack in the trapping rain water problem?
ATo sort the bars by height
BTo store the amount of water trapped at each index
CTo keep track of indices of bars that are not yet bounded by a higher bar on the right
DTo reverse the order of the bars
Attempts:
2 left
💡 Hint
Think about how the stack helps find boundaries for trapping water.
🔧 Debug
advanced
2:00remaining
Identify the Error in Trapping Rain Water Code
What error will this code raise when executed?
DSA Python
def trap(height):
    stack = []
    water = 0
    for i, h in enumerate(height):
        while stack and height[stack[-1]] <= h:
            top = stack.pop()
            if not stack:
                break
            distance = i - stack[-1] - 1
            bounded_height = min(height[stack[-1]], h) - height[top]
            water += distance * bounded_height
        stack.append(i)
    return water

print(trap([4,2,0,3,2,5]))
AInfinite loop
BIndexError due to empty stack access
CTypeError due to invalid operation
DNo error, output is 9
Attempts:
2 left
💡 Hint
Check the condition in the while loop carefully.
Predict Output
advanced
2:00remaining
Output of Modified Trapping Rain Water Code
What is the output of this modified trapping rain water code?
DSA Python
def trap(height):
    stack = []
    water = 0
    for i, h in enumerate(height):
        while stack and height[stack[-1]] < h:
            top = stack.pop()
            if not stack:
                break
            distance = i - stack[-1] - 1
            bounded_height = min(height[stack[-1]], h) - height[top]
            if bounded_height > 0:
                water += distance * bounded_height
        stack.append(i)
    return water

print(trap([4,2,0,3,2,5]))
A10
B9
C8
D7
Attempts:
2 left
💡 Hint
The added check prevents adding negative water amounts.
🚀 Application
expert
3:00remaining
Maximum Water Trapped in Large Histogram
Given a histogram with heights [5,2,1,2,1,5,3,2,4,1], what is the maximum amount of water trapped using the stack method?
A14
B15
C13
D12
Attempts:
2 left
💡 Hint
Visualize the bars and trapped water between the highest boundaries.