Challenge - 5 Problems
Jump Game Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Jump Game Reachability Check
What is the output of this Rust code that checks if you can jump to the last index?
DSA Rust
fn can_jump(nums: Vec<i32>) -> bool { let mut max_reach = 0; for i in 0..nums.len() { if i > max_reach { return false; } max_reach = max_reach.max(i + nums[i] as usize); } true } fn main() { let nums = vec![2,3,1,1,4]; println!("{}", can_jump(nums)); }
Attempts:
2 left
💡 Hint
Think about whether the jumps allow reaching the last index.
✗ Incorrect
The code checks if the maximum reachable index covers the last index. Here, it does, so output is true.
❓ Predict Output
intermediateOutput for Unreachable Last Index
What will this Rust code print when the last index is not reachable?
DSA Rust
fn can_jump(nums: Vec<i32>) -> bool { let mut max_reach = 0; for i in 0..nums.len() { if i > max_reach { return false; } max_reach = max_reach.max(i + nums[i] as usize); } true } fn main() { let nums = vec![3,2,1,0,4]; println!("{}", can_jump(nums)); }
Attempts:
2 left
💡 Hint
Check if the zero blocks progress to the last index.
✗ Incorrect
The zero at index 3 blocks any jump to index 4, so the function returns false.
🔧 Debug
advancedIdentify the bug in Jump Game code
What error will this Rust code produce when compiled and run?
DSA Rust
fn can_jump(nums: Vec<i32>) -> bool { let mut max_reach = 0; for i in 0..nums.len() { if i >= max_reach { return false; } max_reach = max_reach.max(i + nums[i] as usize); } true } fn main() { let nums = vec![2,3,1,1,4]; println!("{}", can_jump(nums)); }
Attempts:
2 left
💡 Hint
Check the condition that returns false inside the loop.
✗ Incorrect
The condition 'i >= max_reach' causes the function to return false even when i == max_reach, which is incorrect. It should be 'i > max_reach'.
❓ Predict Output
advancedOutput of Minimum Jumps Calculation
What is the output of this Rust code that calculates the minimum number of jumps to reach the last index?
DSA Rust
fn jump(nums: Vec<i32>) -> i32 {
let mut jumps = 0;
let mut current_end = 0;
let mut farthest = 0;
for i in 0..nums.len() - 1 {
farthest = farthest.max(i + nums[i] as usize);
if i == current_end {
jumps += 1;
current_end = farthest;
}
}
jumps
}
fn main() {
let nums = vec![2,3,1,1,4];
println!("{}", jump(nums));
}Attempts:
2 left
💡 Hint
Count how many jumps are needed to cover the array.
✗ Incorrect
The minimum jumps to reach the last index is 2: jump from index 0 to 1, then from 1 to 4.
🧠 Conceptual
expertWhy Greedy Works for Jump Game
Why does the greedy approach correctly solve the Jump Game problem?
Attempts:
2 left
💡 Hint
Think about how the algorithm updates the farthest reachable index.
✗ Incorrect
The greedy method updates the farthest reachable index at each step, ensuring that the jumps cover the maximum distance possible, which leads to a correct and efficient solution.
