Skip to main content

23. Jump Game

mediumAsked at Tesla

Decide if you can reach the last index given variable jump sizes — Tesla maps this greedy reachability problem to range estimation for EVs, where each segment has a variable remaining-charge value and you must confirm the destination is reachable before committing a route.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given an integer array nums where nums[i] represents the maximum jump length from index i, return true if you can reach the last index starting from index 0, false otherwise.

Constraints

  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= 10^5

Examples

Example 1

Input
nums = [2,3,1,1,4]
Output
true

Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2

Input
nums = [3,2,1,0,4]
Output
false

Explanation: Every path reaches index 3 where the jump is 0, blocking progress.

Approaches

1. DP boolean array

Maintain a dp[i] array marking reachable indices. For each reachable position, mark all positions it can jump to. O(n^2) worst case.

Time
O(n^2)
Space
O(n)
function canJump(nums) {
  const reachable = new Array(nums.length).fill(false);
  reachable[0] = true;
  for (let i = 0; i < nums.length; i++) {
    if (!reachable[i]) continue;
    for (let j = 1; j <= nums[i] && i + j < nums.length; j++) {
      reachable[i + j] = true;
    }
  }
  return reachable[nums.length - 1];
}

Tradeoff:

2. Greedy max-reach

Track the furthest index reachable at each step. If the current index exceeds the max-reach, stop. One forward pass, constant space.

Time
O(n)
Space
O(1)
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;
}

Tradeoff:

Tesla-specific tips

Tesla's routing team thinks about this as a reachability problem over a charge graph — can a vehicle with degraded battery segments complete a route? They grade on how quickly you get to the greedy insight. Prove to yourself and the interviewer why greedy works here: if maxReach is ever less than the current index, no future position can bridge that gap. Follow-up is often LC 45 (minimum jumps), so know the greedy extension where you greedily pick the index that maximizes the next reach.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Jump Game and other Tesla interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →