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 TypeScript code that calculates trapped rain water using a stack?
DSA Typescript
function trap(height: number[]): number {
let stack: number[] = [];
let water = 0;
for (let i = 0; i < height.length; i++) {
while (stack.length > 0 && height[i] > height[stack[stack.length - 1]]) {
let top = stack.pop()!;
if (stack.length === 0) 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 volume correctly for the given input, which 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 two-pointer approach?
Attempts:
2 left
💡 Hint
Think about how the stack helps find the previous smaller or taller bars.
✗ Incorrect
The stack stores indices of bars in a way that helps find left and right boundaries dynamically while scanning the array once.
🔧 Debug
advancedIdentify the Error in Trapping Rain Water Stack Code
What error will this TypeScript code produce when run?
DSA Typescript
function trap(height: number[]): number {
let stack: number[] = [];
let water = 0;
for (let i = 0; i < height.length; i++) {
while (stack.length > 0 && height[i] >= height[stack[stack.length - 1]]) {
let top = stack.pop();
if (stack.length === 0) 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 top variable is handled after popping from stack.
✗ Incorrect
The code uses a non-null assertion operator (!) correctly after popping from stack, so no TypeError occurs. The output is 9 for the input.
❓ Predict Output
advancedResulting Stack State After Processing Input
What is the state of the stack (indices) after processing the entire input array in this trapping rain water code?
DSA Typescript
function trap(height: number[]): number[] {
let stack: number[] = [];
let water = 0;
for (let i = 0; i < height.length; i++) {
while (stack.length > 0 && height[i] > height[stack[stack.length - 1]]) {
let top = stack.pop()!;
if (stack.length === 0) 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 stack;
}
console.log(trap([3,0,2,0,3]));Attempts:
2 left
💡 Hint
The stack holds indices of bars that have not been fully processed or bounded.
✗ Incorrect
After processing, the stack contains indices 0 and 4 representing bars that form boundaries with no higher bar to the right.
🧠 Conceptual
expertWhy Does the Stack-Based Algorithm Run in O(n) Time?
Why does the stack-based trapping rain water algorithm run in linear time despite having nested loops?
Attempts:
2 left
💡 Hint
Consider how many times each index is pushed and popped from the stack.
✗ Incorrect
Each bar index is pushed once and popped once, so total stack operations are at most 2n, making the algorithm O(n).
