Skip to main content

90. Jump Game II

hardAsked at Salesforce

Find the minimum number of jumps to reach the last index. Salesforce uses this as a greedy stress test — they grade on the BFS-level insight.

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 scheduling for batch-job sequencing.
  • LeetCode Discuss (2025)Hard variant of LC 55.

Problem

You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0]. Each element nums[i] represents the maximum length of a forward jump from index i. Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].

Constraints

  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= 1000
  • It's guaranteed that you can reach nums[n - 1].

Examples

Example 1

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

Explanation: Jump from 0 to 1 (with 3 steps), then from 1 to 4.

Example 2

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

Approaches

1. BFS levels

Each jump opens a new 'level' of reachable indices. Count levels until reaching the end.

Time
O(n)
Space
O(n) worst
// Conceptual — easier to implement as the next variant.

Tradeoff: Conceptually clean.

2. Greedy: track current jump end and farthest

Track currentEnd (max reach of current jump) and farthest (max reach from any seen so far). When i == currentEnd, take a jump and update.

Time
O(n)
Space
O(1)
function jump(nums) {
  let jumps = 0, currentEnd = 0, farthest = 0;
  for (let i = 0; i < nums.length - 1; i++) {
    farthest = Math.max(farthest, i + nums[i]);
    if (i === currentEnd) {
      jumps++;
      currentEnd = farthest;
    }
  }
  return jumps;
}

Tradeoff: O(1) space. The currentEnd / farthest pair tracks 'current BFS level boundary' and 'next BFS level boundary' implicitly.

Salesforce-specific tips

Salesforce uses greedy scheduling for batch-job sequencing. They grade on whether you can articulate the BFS-levels interpretation of the greedy (each iteration extends the BFS frontier). Bonus signal: explain why looping only to n-2 (not n-1) is correct — we don't need to jump from the last index.

Common mistakes

  • Looping to n-1 — would add an extra unnecessary jump.
  • Updating currentEnd before farthest — uses stale data.
  • Confusing 'reach' (max index reachable) with 'jump end' (boundary of current BFS level).

Follow-up questions

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

  • Jump Game (LC 55) — boolean version.
  • Jump Game III (LC 1306).
  • What if jumps have variable costs?

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?

Because at each 'level boundary' (currentEnd), we know the next jump must come — and picking the maximum reach minimizes jumps. There's no benefit to jumping early.

Why loop to n-2?

The last index is the destination; we don't jump from it. Looping to n-1 would increment jumps after reaching the goal.

Practice these live with InterviewChamp.AI

Drill Jump Game II 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 →