📖 Scenario: Imagine you are playing a video game where you need to jump across platforms to reach the end. Each platform lets you jump a certain maximum number of steps forward. You want to check if it's possible to reach the last platform starting from the first one.
🎯 Goal: Build a program that checks if you can jump from the first platform to the last platform using the maximum jump lengths given for each platform.
📋 What You'll Learn
Create an array called jumps with exact values [2, 3, 1, 1, 4]
Create a variable called maxReach and set it to 0
Use a for loop with variable i to iterate over the jumps array
Inside the loop, update maxReach to the maximum of maxReach and i + jumps[i]
If maxReach is less than i at any point before the last index, break the loop
After the loop, print true if maxReach is greater than or equal to the last index, otherwise print false
💡 Why This Matters
🌍 Real World
This problem models real-world scenarios like planning moves in games, robot path planning, or network packet forwarding where you need to check reachability.
💼 Career
Understanding this problem helps in software engineering roles that require problem-solving skills, especially in game development, robotics, and algorithm design.
Progress0 / 4 steps
1
Create the jumps array
Create an array called jumps with these exact values: [2, 3, 1, 1, 4]
DSA Javascript
Hint
Use const jumps = [2, 3, 1, 1, 4]; to create the array.
2
Create maxReach variable
Create a variable called maxReach and set it to 0
DSA Javascript
Hint
Use let maxReach = 0; to create the variable.
3
Implement the jump check loop
Use a for loop with variable i to iterate over the jumps array. Inside the loop, update maxReach to the maximum of maxReach and i + jumps[i]. If maxReach is less than i at any point before the last index, break the loop.
DSA Javascript
Hint
Use for (let i = 0; i < jumps.length; i++) and update maxReach inside the loop.
4
Print if last platform is reachable
After the loop, print true if maxReach is greater than or equal to the last index of jumps, otherwise print false
DSA Javascript
Hint
Use console.log(maxReach >= jumps.length - 1); to print the result.
Practice
(1/5)
1. What does the Jump Game problem ask you to determine given an array of non-negative integers?
easy
A. If you can reach the last index starting from the first index
B. The maximum jump length from any position
C. The minimum number of jumps to reach the last index
D. The sum of all jump lengths in the array
Solution
Step 1: Understand the problem statement
The Jump Game problem asks if it is possible to move from the first index to the last index by jumping according to the values in the array.
Step 2: Identify the goal
The goal is to check if the last index is reachable, not the number of jumps or maximum jump length.
Final Answer:
If you can reach the last index starting from the first index -> Option A
Quick Check:
Jump Game = Reach last index [OK]
Hint: Focus on reachability, not jump counts or sums [OK]
Common Mistakes:
Confusing reachability with minimum jumps
Thinking about maximum jump length only
Assuming sum of jumps matters
2. Which of the following is the correct syntax to check if you can jump from index i to index j in JavaScript, given an array nums?
easy
A. if (nums[i] >= j - i)
B. if (nums[j] <= i + nums[i])
C. if (j < nums[i])
D. if (j <= i + nums[i])
Solution
Step 1: Understand jump condition
You can jump from index i to j if j is within the jump length from i, i.e., j ≤ i + nums[i].
Step 2: Match condition with options
if (j <= i + nums[i]) correctly checks if j ≤ i + nums[i]. Other options compare wrong values or use incorrect operators.
Final Answer:
if (j <= i + nums[i]) -> Option D
Quick Check:
Jump condition = j ≤ i + nums[i] [OK]
Hint: Check if target index ≤ current index + jump length [OK]
Common Mistakes:
Comparing nums[j] instead of j
Using < instead of ≤
Mixing indices and values incorrectly
3. What will be the output of the following JavaScript code?
function canJump(nums) {
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]));
console.log(canJump([3,2,1,0,4]));
medium
A. true false
B. true true
C. false true
D. false false
Solution
Step 1: Trace first input [2,3,1,1,4]
maxReach updates as: i=0 maxReach=2, i=1 maxReach=4, i=2 maxReach=4, i=3 maxReach=4, i=4 maxReach=8. No i > maxReach, so returns true.
Hint: Track max reachable index; fail if current index exceeds it [OK]
Common Mistakes:
Not updating maxReach correctly
Returning true too early
Ignoring the condition i > maxReach
4. Identify the bug in the following code that attempts to solve the Jump Game problem:
function canJump(nums) {
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;
}
medium
A. maxReach should be initialized to 1, not 0
B. The loop condition should be i < nums.length, not i <= nums.length
C. The return true statement should be inside the loop
D. The comparison should be i >= maxReach instead of i > maxReach
Solution
Step 1: Analyze loop boundary
The loop runs from i = 0 to i <= nums.length, which means it accesses nums[nums.length], causing undefined access.
Step 2: Correct loop condition
The loop should run while i < nums.length to avoid out-of-bounds errors.
Final Answer:
The loop condition should be i < nums.length, not i <= nums.length -> Option B
Quick Check:
Loop boundary off by one error [OK]
Hint: Check array indices carefully to avoid out-of-bounds [OK]
Common Mistakes:
Using <= instead of < in loops
Misinitializing maxReach
Placing return true inside loop
5. Given the array nums = [1,2,0,1,0,2,0], which of the following statements is true about reaching the last index using the Jump Game logic?
hard
A. You can reach the last index only if you jump exactly 2 steps each time
B. You can reach the last index because jumps can skip zeros if reachable
C. You cannot reach the last index because zeros block all paths
D. You can reach the last index only if you jump backward at some point