0
0
DSA Pythonprogramming~20 mins

Trapping Rain Water Problem in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trapping 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?
DSA Python
def trap(height):
    left, right = 0, len(height) - 1
    left_max, right_max = 0, 0
    water = 0
    while left < right:
        if height[left] < height[right]:
            if height[left] >= left_max:
                left_max = height[left]
            else:
                water += left_max - height[left]
            left += 1
        else:
            if height[right] >= right_max:
                right_max = height[right]
            else:
                water += right_max - height[right]
            right -= 1
    return water

print(trap([0,1,0,2,1,0,1,3,2,1,2,1]))
A8
B5
C6
D7
Attempts:
2 left
💡 Hint
Think about how water is trapped between bars of different heights.
🧠 Conceptual
intermediate
1:30remaining
Understanding the role of left_max and right_max
In the trapping rain water problem, what do the variables left_max and right_max represent?
AThe current water trapped at left and right positions
BThe highest bars seen so far from the left and right sides respectively
CThe total water trapped so far from left and right
DThe indexes of the tallest bars in the array
Attempts:
2 left
💡 Hint
Think about how water trapping depends on the tallest bars on each side.
Predict Output
advanced
2:30remaining
Output of modified trapping rain water code with extra print statements
What is the output of this code snippet that prints trapped water after each step?
DSA Python
def trap(height):
    left, right = 0, len(height) - 1
    left_max, right_max = 0, 0
    water = 0
    steps = []
    while left < right:
        if height[left] < height[right]:
            if height[left] >= left_max:
                left_max = height[left]
            else:
                water += left_max - height[left]
            steps.append(water)
            left += 1
        else:
            if height[right] >= right_max:
                right_max = height[right]
            else:
                water += right_max - height[right]
            steps.append(water)
            right -= 1
    return steps

print(trap([4,2,0,3,2,5]))
A[0, 2, 6, 7, 9]
B[0, 2, 5, 7, 8]
C[0, 1, 3, 6, 7]
D[0, 2, 4, 7, 7]
Attempts:
2 left
💡 Hint
Trace the water accumulation step by step.
🔧 Debug
advanced
2:00remaining
Identify the error in this trapping rain water code
What error does this code produce when run?
DSA Python
def trap(height):
    left, right = 0, len(height) - 1
    left_max, right_max = 0, 0
    water = 0
    while left <= right:
        if height[left] < height[right]:
            if height[left] >= left_max:
                left_max = height[left]
            else:
                water += left_max - height[left]
            left += 1
        else:
            if height[right] >= right_max:
                right_max = height[right]
            else:
                water += right_max - height[right]
            right -= 1
    return water

print(trap([0,1,0,2,1,0,1,3,2,1,2,1]))
AIndexError: list index out of range
BRuns correctly and returns 6
CTypeError: unsupported operand type(s)
DReturns incorrect trapped water value
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
🚀 Application
expert
3:00remaining
Maximum trapped water between two bars
Given an array representing heights of bars, which pair of bars traps the maximum water between them (ignoring bars in between)?
DSA Python
def max_water_between_bars(height):
    max_water = 0
    left_index = 0
    right_index = 0
    for i in range(len(height)):
        for j in range(i+1, len(height)):
            water = min(height[i], height[j]) * (j - i - 1)
            if water > max_water:
                max_water = water
                left_index, right_index = i, j
    return (left_index, right_index, max_water)

print(max_water_between_bars([3,1,2,4,0,1,3,2]))
A(0, 6, 15)
B(3, 7, 8)
C(1, 6, 10)
D(0, 3, 6)
Attempts:
2 left
💡 Hint
Calculate water trapped as height times distance between bars minus one.