0
0
DSA Pythonprogramming~10 mins

Trapping Rain Water Using Stack 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 an empty stack for storing indices.

DSA Python
stack = [1]
Drag options to blanks, or click blank then click option'
A[]
B{}
C()
Dset()
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} creates a dictionary, not a stack.
Using parentheses () creates a tuple, which is immutable.
2fill in blank
medium

Complete the code to pop the top element from the stack.

DSA Python
top = stack.[1]()
Drag options to blanks, or click blank then click option'
Apop
Bremove
Cdelete
Ddiscard
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove() causes errors because it expects a value, not an index.
Using delete is not a list method.
3fill in blank
hard

Fix the error in the condition to check if the stack is not empty.

DSA Python
while stack and height[i] > height[[1]]:
Drag options to blanks, or click blank then click option'
Astack.pop()
Bstack[0]
Cstack
Dstack[-1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using stack[0] compares with the bottom element, causing wrong logic.
Using stack.pop() removes the element, which is incorrect here.
4fill in blank
hard

Fill both blanks to calculate the distance and bounded height for trapped water.

DSA Python
distance = i - [1] - 1
bounded_height = min(height[i], height[[2]]) - height[top]
Drag options to blanks, or click blank then click option'
Astack[-1]
Btop
Cstack[0]
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices for distance causes incorrect water calculation.
Mixing up top and stack[-1] leads to errors.
5fill in blank
hard

Fill all three blanks to update the total trapped water and push current index to stack.

DSA Python
water += distance * bounded_height
stack.[1](i)
i += [2]

# Loop condition
while i < [3]:
Drag options to blanks, or click blank then click option'
Aappend
B1
Clen(height)
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of append removes elements incorrectly.
Incrementing i by wrong value causes infinite loops.
Looping beyond length causes index errors.