Challenge - 5 Problems
Trapping 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 for the given height array?
DSA Rust
fn trap(height: Vec<i32>) -> i32 {
let n = height.len();
let mut left_max = vec![0; n];
let mut right_max = vec![0; n];
let mut water = 0;
if n == 0 {
return 0;
}
left_max[0] = height[0];
for i in 1..n {
left_max[i] = left_max[i - 1].max(height[i]);
}
right_max[n - 1] = height[n - 1];
for i in (0..n - 1).rev() {
right_max[i] = right_max[i + 1].max(height[i]);
}
for i in 0..n {
water += left_max[i].min(right_max[i]) - height[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 left_max and right_max arrays help find trapped water at each position.
✗ Incorrect
The code calculates the trapped water by finding the maximum height to the left and right of each bar, then sums the minimum of these two maxima minus the current height. For the given input, the total trapped water is 6 units.
🧠 Conceptual
intermediateUnderstanding the role of left_max and right_max arrays
In the trapping rain water problem, what do the left_max and right_max arrays represent?
Attempts:
2 left
💡 Hint
Think about how to find the boundaries that trap water at each position.
✗ Incorrect
left_max[i] stores the tallest bar from the left up to i, and right_max[i] stores the tallest bar from the right up to i. These help determine the water trapped at position i.
🔧 Debug
advancedIdentify the error in this Rust code for trapping rain water
What error will this Rust code produce when run?
DSA Rust
fn trap(height: Vec<i32>) -> i32 {
let n = height.len();
let mut left_max = vec![0; n];
let mut right_max = vec![0; n];
let mut water = 0;
if n == 0 {
return 0;
}
left_max[0] = height[0];
for i in 1..n {
left_max[i] = left_max[i - 1].max(height[i]);
}
right_max[n - 1] = height[n - 1];
for i in (0..n - 1).rev() {
right_max[i] = right_max[i + 1].max(height[i]);
}
for i in 0..n {
water += left_max[i].min(right_max[i]) - height[i];
}
water
}
fn main() {
let height = vec![4,2,0,3,2,5];
println!("{}", trap(height));
}Attempts:
2 left
💡 Hint
Check array indexing carefully, especially at boundaries.
✗ Incorrect
The code tries to assign right_max[n], but valid indices are 0 to n-1. This causes an index out of bounds panic at runtime.
🚀 Application
advancedCalculate trapped water for a custom height array
Given the height array [3, 0, 2, 0, 4], what is the total trapped rain water?
Attempts:
2 left
💡 Hint
Visualize the bars and the water trapped between them.
✗ Incorrect
The trapped water units are 3 at position 1, 1 at position 2, and 3 at position 3, totaling 7.
🧠 Conceptual
expertTime complexity of the trapping rain water algorithm using left_max and right_max arrays
What is the time complexity of the trapping rain water solution that uses left_max and right_max arrays?
Attempts:
2 left
💡 Hint
Consider how many times the array is traversed.
✗ Incorrect
The algorithm makes three passes over the array: one to build left_max, one for right_max, and one to calculate trapped water, all linear time.
