Challenge - 5 Problems
Stack Rain Water Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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]))
Attempts:
2 left
💡 Hint
Think about how the stack helps find boundaries to trap water between bars.
✗ Incorrect
The code uses a stack to track indices of bars. When a higher bar is found, it calculates trapped water between the current bar and the last bar in the stack. The total trapped water for the input is 6 units.
🧠 Conceptual
intermediate1:30remaining
Understanding Stack Usage in Trapping Rain Water
Why do we use a stack in the trapping rain water problem?
Attempts:
2 left
💡 Hint
Think about how the stack helps find boundaries for trapping water.
✗ Incorrect
The stack stores indices of bars that have not yet found a higher bar on the right side. When a higher bar appears, it helps calculate trapped water between bars.
🔧 Debug
advanced2: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]))
Attempts:
2 left
💡 Hint
Check the condition in the while loop carefully.
✗ Incorrect
The code uses <= in the while condition, which still works correctly and does not cause errors. The output for the input is 9 units of trapped water.
❓ Predict Output
advanced2: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]))
Attempts:
2 left
💡 Hint
The added check prevents adding negative water amounts.
✗ Incorrect
The added if condition avoids adding negative trapped water. The total trapped water remains 9 units for the input.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Visualize the bars and trapped water between the highest boundaries.
✗ Incorrect
The maximum trapped water calculated by the stack method for the given heights is 14 units.