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 Rust code that calculates trapped rain water using a stack approach?
DSA Rust
fn trap(height: Vec<i32>) -> i32 {
let mut stack: Vec<usize> = Vec::new();
let mut water = 0;
for i in 0..height.len() {
while !stack.is_empty() && height[i] > height[*stack.last().unwrap()] {
let top = stack.pop().unwrap();
if stack.is_empty() {
break;
}
let distance = i - stack.last().unwrap() - 1;
let bounded_height = std::cmp::min(height[i], height[*stack.last().unwrap()]) - height[top];
water += (distance as i32) * bounded_height;
}
stack.push(i);
}
water
}
fn main() {
let height = vec![0,1,0,2,1,0,1,3,2,1,2,1];
println!("{}", trap(height));
}Attempts:
2 left
💡 Hint
Think about how the stack helps find boundaries to trap water between bars.
✗ Incorrect
The code uses a stack to keep 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
intermediateStack role in trapping rain water algorithm
In the stack-based trapping rain water algorithm, what does the stack primarily store during execution?
Attempts:
2 left
💡 Hint
Consider how the algorithm finds boundaries to trap water.
✗ Incorrect
The stack stores indices of bars in non-increasing height order to find left boundaries for trapped water when a higher bar is encountered.
🔧 Debug
advancedIdentify the error in this trapping rain water stack code
What error will this Rust code produce when calculating trapped rain water using a stack?
DSA Rust
fn trap(height: Vec<i32>) -> i32 {
let mut stack: Vec<usize> = Vec::new();
let mut water = 0;
for i in 0..height.len() {
while !stack.is_empty() && height[i] >= height[*stack.last().unwrap()] {
let top = stack.pop().unwrap();
if stack.is_empty() {
break;
}
let distance = i - stack.last().unwrap() - 1;
let bounded_height = std::cmp::min(height[i], height[*stack.last().unwrap()]) - height[top];
water += (distance as i32) * bounded_height;
}
stack.push(i);
}
water
}
fn main() {
let height = vec![4,2,0,3,2,5];
println!("{}", trap(height));
}Attempts:
2 left
💡 Hint
Check the comparison operator in the while condition.
✗ Incorrect
Using '>=' instead of '>' causes the algorithm to miscalculate trapped water, resulting in output 8 instead of the correct 9.
❓ Predict Output
advancedOutput of modified trapping rain water code with empty input
What is the output of this Rust code when the input height vector is empty?
DSA Rust
fn trap(height: Vec<i32>) -> i32 {
let mut stack: Vec<usize> = Vec::new();
let mut water = 0;
for i in 0..height.len() {
while !stack.is_empty() && height[i] > height[*stack.last().unwrap()] {
let top = stack.pop().unwrap();
if stack.is_empty() {
break;
}
let distance = i - stack.last().unwrap() - 1;
let bounded_height = std::cmp::min(height[i], height[*stack.last().unwrap()]) - height[top];
water += (distance as i32) * bounded_height;
}
stack.push(i);
}
water
}
fn main() {
let height = vec![];
println!("{}", trap(height));
}Attempts:
2 left
💡 Hint
Consider what happens when the input vector is empty.
✗ Incorrect
With empty input, the loop does not run and water remains 0. No unwrap on empty stack occurs because the while condition is never true.
🧠 Conceptual
expertWhy does the stack-based trapping rain water algorithm run in O(n) time?
Choose the best explanation for why the stack-based trapping rain water algorithm has linear time complexity.
Attempts:
2 left
💡 Hint
Think about how many times each index is processed in the stack.
✗ Incorrect
Each index is pushed once and popped once from the stack, so total stack operations are at most 2n, making the algorithm O(n).
