0
0
DSA Pythonprogramming~10 mins

Trapping Rain Water Problem in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the total trapped water to zero.

DSA Python
water = [1]
Drag options to blanks, or click blank then click option'
A0
B1
CNone
D[]
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing water with 1 or None instead of 0.
2fill in blank
medium

Complete the code to find the maximum height to the left of the current bar.

DSA Python
left_max = max(height[:[1]])
Drag options to blanks, or click blank then click option'
Ai
Bi+1
Ci-1
Dlen(height)
Attempts:
3 left
💡 Hint
Common Mistakes
Using i+1 includes the current bar, which is incorrect.
3fill in blank
hard

Fix the error in the code to calculate trapped water at index i.

DSA Python
water += min(left_max, right_max) - [1]
Drag options to blanks, or click blank then click option'
Aleft_max
Bright_max
Cheight[i]
Dwater
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting left_max or right_max instead of height[i].
4fill in blank
hard

Fill both blanks to complete the loop that iterates over the bars except the first and last.

DSA Python
for i in range([1], [2]):
Drag options to blanks, or click blank then click option'
A1
B0
Clen(height) - 1
Dlen(height)
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 0 or ending at len(height) includes bars that can't trap water.
5fill in blank
hard

Fill all three blanks to complete the function that calculates trapped rain water.

DSA Python
def trap(height):
    water = [1]
    for i in range([2], [3]):
        left_max = max(height[:i])
        right_max = max(height[i+1:])
        water += min(left_max, right_max) - height[i]
    return water
Drag options to blanks, or click blank then click option'
A0
B1
Clen(height) - 1
Dlen(height)
Attempts:
3 left
💡 Hint
Common Mistakes
Incorrect loop range or water initialization.