Challenge - 5 Problems
Stack Rain Water 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 JavaScript code that calculates trapped rain water using a stack?
DSA Javascript
function trap(height) {
let stack = [];
let water = 0;
for (let i = 0; i < height.length; i++) {
while (stack.length && height[i] > height[stack[stack.length - 1]]) {
let top = stack.pop();
if (!stack.length) break;
let distance = i - stack[stack.length - 1] - 1;
let bounded_height = Math.min(height[i], height[stack[stack.length - 1]]) - height[top];
water += distance * bounded_height;
}
stack.push(i);
}
return water;
}
console.log(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 find the left and right boundaries for trapped water. It calculates the trapped water between bars by popping the stack and measuring the distance and height difference. For the 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 the stack helps find the left boundary for water trapping.
✗ Incorrect
The stack stores indices of bars that are potential boundaries. When a higher bar is found, it helps calculate trapped water between the current bar and the previous boundary efficiently.
❓ Predict Output
advancedOutput of Modified Trapping Rain Water Code
What is the output of this modified JavaScript code that calculates trapped rain water using a stack?
DSA Javascript
function trap(height) {
let stack = [];
let water = 0;
for (let i = 0; i < height.length; i++) {
while (stack.length && height[i] >= height[stack[stack.length - 1]]) {
let top = stack.pop();
if (!stack.length) break;
let distance = i - stack[stack.length - 1] - 1;
let bounded_height = Math.min(height[i], height[stack[stack.length - 1]]) - height[top];
water += distance * bounded_height;
}
stack.push(i);
}
return water;
}
console.log(trap([4,2,0,3,2,5]));Attempts:
2 left
💡 Hint
Check how the condition height[i] >= height[stack top] affects the calculation.
✗ Incorrect
The code calculates trapped water as 9 units for the input. The '>=' condition allows popping bars of equal height, correctly calculating trapped water.
🔧 Debug
advancedIdentify the Error in Trapping Rain Water Code
What error will this JavaScript code produce when calculating trapped rain water?
DSA Javascript
function trap(height) {
let stack = [];
let water = 0;
for (let i = 0; i < height.length; i++) {
while (stack.length && height[i] > height[stack[stack.length - 1]]) {
let top = stack.pop();
if (!stack.length) break;
let distance = i - stack[stack.length - 1] - 1;
let bounded_height = Math.min(height[i], height[stack[stack.length - 1]]) - height[top];
water += distance * bounded_height;
}
stack.push(i);
}
return water;
}
console.log(trap([0,1,0,2,1,0,1,3,2,1,2,1]));Attempts:
2 left
💡 Hint
Check the stack index used inside the while condition.
✗ Incorrect
The code uses stack[stack.length] which is undefined (index out of range). height[undefined] is undefined, so height[i] > undefined is always false. The while loop never executes its body, the stack never pops, water remains 0, and no runtime error occurs.
🧠 Conceptual
expertWhy Does the Stack-Based Algorithm Work for Trapping Rain Water?
Which explanation best describes why the stack-based algorithm correctly calculates trapped rain water?
Attempts:
2 left
💡 Hint
Think about how the stack helps identify boundaries for water trapping.
✗ Incorrect
The stack stores indices of bars that form boundaries. When a higher bar is found, it calculates trapped water between the current bar and the previous taller bar on the stack, ensuring correct water volume calculation.
