Challenge - 5 Problems
Stack Rainwater Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Trapping Rain Water Calculation
What is the output of the following Java code that calculates trapped rain water using a stack?
DSA Java
import java.util.Stack; public class RainWater { public static int trap(int[] height) { Stack<Integer> stack = new Stack<>(); int water = 0, current = 0; while (current < height.length) { while (!stack.isEmpty() && height[current] > height[stack.peek()]) { int top = stack.pop(); if (stack.isEmpty()) break; int distance = current - stack.peek() - 1; int bounded_height = Math.min(height[current], height[stack.peek()]) - height[top]; water += distance * bounded_height; } stack.push(current++); } return water; } public static void main(String[] args) { int[] height = {0,1,0,2,1,0,1,3,2,1,2,1}; System.out.println(trap(height)); } }
Attempts:
2 left
💡 Hint
Trace the stack operations and calculate trapped water between bars.
✗ Incorrect
The code uses a stack to find boundaries and calculates trapped water between them. For the given input, the total trapped water is 6 units.
🧠 Conceptual
intermediateUnderstanding Stack Usage in Trapping Rain Water
Why is a stack used in the trapping rain water problem instead of a simple loop?
Attempts:
2 left
💡 Hint
Think about how boundaries for water trapping are identified.
✗ Incorrect
The stack helps keep indices of bars that might form boundaries. When a higher bar is found, it helps calculate trapped water between boundaries.
🔧 Debug
advancedIdentify the Error in Modified Trapping Rain Water Code
What error will occur when running this modified Java code for trapping rain water?
DSA Java
import java.util.Stack; public class RainWater { public static int trap(int[] height) { Stack<Integer> stack = new Stack<>(); int water = 0, current = 0; while (current <= height.length) { while (!stack.isEmpty() && height[current] > height[stack.peek()]) { int top = stack.pop(); if (stack.isEmpty()) break; int distance = current - stack.peek() - 1; int bounded_height = Math.min(height[current], height[stack.peek()]) - height[top]; water += distance * bounded_height; } stack.push(current++); } return water; } public static void main(String[] args) { int[] height = {4,2,0,3,2,5}; System.out.println(trap(height)); } }
Attempts:
2 left
💡 Hint
Check the loop condition and array access.
✗ Incorrect
The loop condition uses <= height.length which causes current to go out of array bounds, leading to ArrayIndexOutOfBoundsException.
🚀 Application
advancedCalculate Trapped Water for Custom Input
Given the height array {3, 0, 2, 0, 4}, what is the total trapped rain water calculated by the stack method?
Attempts:
2 left
💡 Hint
Visualize the bars and trapped water between them.
✗ Incorrect
Water trapped is 7 units in total between the bars.
❓ Predict Output
expertOutput of Trapping Rain Water with Edge Case Input
What is the output of the following Java code for trapping rain water when input is strictly increasing heights?
DSA Java
import java.util.Stack; public class RainWater { public static int trap(int[] height) { Stack<Integer> stack = new Stack<>(); int water = 0, current = 0; while (current < height.length) { while (!stack.isEmpty() && height[current] > height[stack.peek()]) { int top = stack.pop(); if (stack.isEmpty()) break; int distance = current - stack.peek() - 1; int bounded_height = Math.min(height[current], height[stack.peek()]) - height[top]; water += distance * bounded_height; } stack.push(current++); } return water; } public static void main(String[] args) { int[] height = {1, 2, 3, 4, 5}; System.out.println(trap(height)); } }
Attempts:
2 left
💡 Hint
Strictly increasing bars cannot trap water.
✗ Incorrect
Since bars increase strictly, no water can be trapped between them, so output is 0.
