Challenge - 5 Problems
Jump Game Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Jump Game Reachability Check
What is the output of this TypeScript code that checks if you can jump to the last index?
DSA Typescript
function canJump(nums: number[]): boolean {
let maxReach = 0;
for (let i = 0; i < nums.length; i++) {
if (i > maxReach) return false;
maxReach = Math.max(maxReach, i + nums[i]);
}
return true;
}
console.log(canJump([2,3,1,1,4]));Attempts:
2 left
💡 Hint
Think about whether the jumps allow reaching the last index.
✗ Incorrect
The function tracks the furthest index reachable. Since the input allows jumps to the end, it returns true.
❓ Predict Output
intermediate2:00remaining
Output when jump is blocked early
What does this code print when the jump is blocked early?
DSA Typescript
function canJump(nums: number[]): boolean {
let maxReach = 0;
for (let i = 0; i < nums.length; i++) {
if (i > maxReach) return false;
maxReach = Math.max(maxReach, i + nums[i]);
}
return true;
}
console.log(canJump([3,2,1,0,4]));Attempts:
2 left
💡 Hint
Check if the zero blocks further progress.
✗ Incorrect
The zero at index 3 blocks reaching index 4, so the function returns false.
🔧 Debug
advanced2:00remaining
Identify the error in jump game code
What error does this code produce when run?
DSA Typescript
function canJump(nums: number[]): boolean {
let maxReach = 0;
for (let i = 0; i <= nums.length; i++) {
if (i > maxReach) return false;
maxReach = Math.max(maxReach, i + nums[i]);
}
return true;
}
console.log(canJump([2,3,1,1,4]));Attempts:
2 left
💡 Hint
Check the loop boundary and array access.
✗ Incorrect
The loop goes one index too far (i <= nums.length), accessing nums[nums.length] which is undefined. This leads to NaN in the maxReach calculation (since Math.max with NaN returns NaN), and attempting to access nums[i] when i is out of bounds causes a TypeError.
🧠 Conceptual
advanced2:00remaining
Minimum jumps to reach the end
Given an array where each element represents max jump length at that position, what is the minimum number of jumps to reach the last index for [2,3,1,1,4]?
Attempts:
2 left
💡 Hint
Try to jump the farthest possible at each step.
✗ Incorrect
Jump from index 0 to 1, then from 1 to 4, total 2 jumps.
🚀 Application
expert2:00remaining
Output of jump game with zero at start
What is the output of this code when the first element is zero?
DSA Typescript
function canJump(nums: number[]): boolean {
let maxReach = 0;
for (let i = 0; i < nums.length; i++) {
if (i > maxReach) return false;
maxReach = Math.max(maxReach, i + nums[i]);
}
return true;
}
console.log(canJump([0,1,2]));Attempts:
2 left
💡 Hint
If you cannot move from the start, you cannot reach the end.
✗ Incorrect
Since the first element is zero, you cannot move anywhere, so the function returns false.