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 the following Java code that checks if you can jump to the last index?
DSA Java
public class JumpGame { public static boolean canJump(int[] nums) { int maxReach = 0; for (int i = 0; i < nums.length; i++) { if (i > maxReach) return false; maxReach = Math.max(maxReach, i + nums[i]); } return true; } public static void main(String[] args) { int[] nums = {2,3,1,1,4}; System.out.println(canJump(nums)); } }
Attempts:
2 left
💡 Hint
Trace the maximum reachable index as you iterate through the array.
✗ Incorrect
The code checks if the current index is reachable. Since maxReach updates to cover the last index, the output is true.
❓ Predict Output
intermediateOutput of Jump Game with Unreachable Last Index
What will the following Java code print when the last index is not reachable?
DSA Java
public class JumpGame { public static boolean canJump(int[] nums) { int maxReach = 0; for (int i = 0; i < nums.length; i++) { if (i > maxReach) return false; maxReach = Math.max(maxReach, i + nums[i]); } return true; } public static void main(String[] args) { int[] nums = {3,2,1,0,4}; System.out.println(canJump(nums)); } }
Attempts:
2 left
💡 Hint
Check if the loop breaks early due to an unreachable index.
✗ Incorrect
At index 3, maxReach is 3 but nums[3] is 0, so maxReach does not advance beyond 3, making index 4 unreachable.
🔧 Debug
advancedIdentify the bug in Jump Game code
What error will the following Java code produce when run?
DSA Java
public class JumpGame { public static boolean canJump(int[] nums) { int maxReach = 0; for (int i = 0; i <= nums.length; i++) { if (i > maxReach) return false; maxReach = Math.max(maxReach, i + nums[i]); } return true; } public static void main(String[] args) { int[] nums = {2,3,1,1,4}; System.out.println(canJump(nums)); } }
Attempts:
2 left
💡 Hint
Check the loop condition and array access.
✗ Incorrect
The loop runs with i <= nums.length causing access to nums[nums.length], which is out of bounds.
🧠 Conceptual
advancedMinimum Jumps to Reach Last Index
Given an array where each element represents the max jump length at that position, which approach correctly finds the minimum number of jumps to reach the last index?
Attempts:
2 left
💡 Hint
Think about how to jump as far as possible with each jump.
✗ Incorrect
The greedy approach efficiently tracks the furthest reachable index within the current jump and increments jumps when moving beyond it.
🚀 Application
expertOutput of Modified Jump Game with Path Tracking
What is the output of the following Java code that returns the path of indices to reach the last index if possible, else returns an empty list?
DSA Java
import java.util.*; public class JumpGame { public static List<Integer> jumpPath(int[] nums) { int n = nums.length; List<Integer> path = new ArrayList<>(); int maxReach = 0, stepEnd = 0, lastPos = 0; for (int i = 0; i < n - 1; i++) { maxReach = Math.max(maxReach, i + nums[i]); if (i == stepEnd) { path.add(lastPos); lastPos = maxReach; stepEnd = maxReach; if (stepEnd >= n - 1) break; } } if (stepEnd < n - 1) return new ArrayList<>(); path.add(n - 1); return path; } public static void main(String[] args) { int[] nums = {2,3,1,1,4}; System.out.println(jumpPath(nums)); } }
Attempts:
2 left
💡 Hint
Check how the path list is updated when stepEnd is reached.
✗ Incorrect
The code adds lastPos to path when reaching stepEnd: adds 0 at i=0, adds 2 at i=2 (after maxReach updates to 4 at i=1), then adds 4, resulting in [0, 2, 4].
