Skip to main content

50. Jump Game

mediumAsked at Salesforce

Determine if you can reach the last index of an array where each element is the max jump distance. Salesforce uses this as the canonical greedy problem.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce uses greedy patterns in their task-scheduling algorithms.
  • LeetCode Discuss (2025-10)Common pairing with LC 45 (Jump Game II).

Problem

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position. Return true if you can reach the last index, or 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

Example 2

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

Approaches

1. DP with reachable[]

reachable[i] = can we reach i? reachable[i] = exists j < i where reachable[j] AND j + nums[j] >= i.

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

Tradeoff: O(n^2). Salesforce wants O(n) greedy.

2. Greedy: track max reachable

Walk left to right. Track the farthest index reachable so far. If i ever exceeds it, return false.

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]);
    if (maxReach >= nums.length - 1) return true;
  }
  return true;
}

Tradeoff: Greedy. The invariant is: if i <= maxReach, we can reach i. If we reach the end, we win. If i ever exceeds maxReach, we're stuck.

Salesforce-specific tips

Salesforce uses this greedy pattern in their task-scheduling algorithms (how far ahead can we plan given current resource availability). They grade on whether you spot greedy is optimal vs reaching for DP. Bonus signal: discuss that LC 45 (Jump Game II — minimize jumps) extends this with a similar greedy but tracking 'current jump end'.

Common mistakes

  • Trying BFS/DFS — works but O(n^2) and overkill.
  • Forgetting the early termination — slower than necessary on inputs that reach the end fast.
  • Off-by-one on the target (it's nums.length - 1, not nums.length).

Follow-up questions

An interviewer at Salesforce may pivot to one of these next:

  • Jump Game II (LC 45) — minimum number of jumps.
  • Jump Game III (LC 1306) — BFS on jumpable positions.
  • Jump Game IV (LC 1345) — graph BFS with same-value edges.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why is greedy correct here?

Because any index i within maxReach is achievable, and extending maxReach is always equally or more permissive. There's no reason to ever pick a suboptimal jump.

What if I track minReach instead?

Doesn't help — minReach is always i. The interesting question is maxReach: what's the farthest we can go.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →