Challenge - 5 Problems
Container With Most Water Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Two-Pointer Container Calculation
What is the output of this Rust code that calculates the maximum water container area using two pointers?
DSA Rust
fn max_area(height: Vec<i32>) -> i32 {
let (mut left, mut right) = (0, height.len() - 1);
let mut max_area = 0;
while left < right {
let width = (right - left) as i32;
let current_area = width * std::cmp::min(height[left], height[right]);
if current_area > max_area {
max_area = current_area;
}
if height[left] < height[right] {
left += 1;
} else {
right -= 1;
}
}
max_area
}
fn main() {
let heights = vec![1,8,6,2,5,4,8,3,7];
println!("{}", max_area(heights));
}Attempts:
2 left
💡 Hint
Think about how the two pointers move and how the area is calculated at each step.
✗ Incorrect
The maximum area is found between the lines at positions 1 and 8 (0-based), heights 8 and 7, width 7, min-height 7, area 49. The code correctly computes this using the two-pointer approach.
❓ Predict Output
intermediateResult of Container Area with Equal Heights
What will be printed by this Rust program that calculates the maximum water container area?
DSA Rust
fn max_area(height: Vec<i32>) -> i32 {
let (mut left, mut right) = (0, height.len() - 1);
let mut max_area = 0;
while left < right {
let width = (right - left) as i32;
let current_area = width * std::cmp::min(height[left], height[right]);
if current_area > max_area {
max_area = current_area;
}
if height[left] < height[right] {
left += 1;
} else {
right -= 1;
}
}
max_area
}
fn main() {
let heights = vec![5,5,5,5,5];
println!("{}", max_area(heights));
}Attempts:
2 left
💡 Hint
All heights are equal, so the area depends on the width between pointers.
✗ Incorrect
With all heights equal to 5, the maximum area is height 5 times width 4 (between indices 0 and 4), which is 20.
🔧 Debug
advancedIdentify the Error in Container Calculation Code
This Rust code is intended to find the maximum water container area but has a bug. What error will it cause when run?
DSA Rust
fn max_area(height: Vec<i32>) -> i32 {
let (mut left, mut right) = (0, height.len());
let mut max_area = 0;
while left < right {
let width = (right - left) as i32;
let current_area = width * std::cmp::min(height[left], height[right]);
if current_area > max_area {
max_area = current_area;
}
if height[left] < height[right] {
left += 1;
} else {
right -= 1;
}
}
max_area
}
fn main() {
let heights = vec![1,8,6,2,5,4,8,3,7];
println!("{}", max_area(heights));
}Attempts:
2 left
💡 Hint
Check how the right pointer is initialized and used as an index.
✗ Incorrect
The right pointer is initialized to height.len(), which is out of bounds for indexing. Accessing height[right] causes a panic at runtime.
🧠 Conceptual
advancedWhy Two-Pointer Approach Works for Container With Most Water
Why does the two-pointer approach correctly find the maximum water container area in linear time?
Attempts:
2 left
💡 Hint
Think about how the area depends on the shorter line and width.
✗ Incorrect
The area is limited by the shorter line. Moving the pointer at the shorter line inward may find a taller line and possibly a larger area. This approach avoids checking all pairs and runs in linear time.
🚀 Application
expertMaximum Water Container Area for Large Input
Given a vector of 1,000,000 heights where each height is 1, what is the maximum water container area computed by the two-pointer method?
DSA Rust
fn max_area(height: Vec<i32>) -> i32 {
let (mut left, mut right) = (0, height.len() - 1);
let mut max_area = 0;
while left < right {
let width = (right - left) as i32;
let current_area = width * std::cmp::min(height[left], height[right]);
if current_area > max_area {
max_area = current_area;
}
if height[left] < height[right] {
left += 1;
} else {
right -= 1;
}
}
max_area
}
fn main() {
let heights = vec![1; 1_000_000];
println!("{}", max_area(heights));
}Attempts:
2 left
💡 Hint
All heights are equal, so the maximum area is height times the maximum width.
✗ Incorrect
With all heights equal to 1, the maximum area is height 1 times width 999999 (between indices 0 and 999999).
