Skip to main content

91. Jump Game II

mediumAsked at Snowflake

Find the minimum number of jumps to reach the last index. Snowflake asks this for BFS-style greedy with level boundaries — relevant to estimating the minimum number of repartition steps in distributed plan execution.

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

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2025-Q4)Snowflake execution-engine team uses this in onsites.
  • LeetCode Discuss (2025-10)Reported at Snowflake SDE-II screens.

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].

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

Example 2

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

Approaches

1. DP min-jumps

dp[i] = min jumps to reach i. Update dp[j] for j in [i+1, i+nums[i]].

Time
O(n * maxJump)
Space
O(n)
// outline — slower than BFS-style.

Tradeoff: Works but worse than the greedy version.

2. BFS-style greedy (optimal)

Track currentEnd (farthest reachable in the current jump count) and farthest (farthest reachable in one more jump). Increment jumps when you cross currentEnd.

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(n). The 'levels' are implicit BFS levels — each jump extends to the farthest reachable from the current level.

Snowflake-specific tips

Snowflake interviewers want the BFS-level interpretation: 'jumps == BFS depth'. Bonus signal: connect to distributed query execution — each shuffle/repartition step is a 'jump' across compute nodes, and minimizing repartitions has the same flavor.

Common mistakes

  • Looping to nums.length instead of nums.length - 1 — fires an extra jump at the end.
  • Updating jumps before crossing currentEnd — over-counts.
  • Confusing this with Jump Game I (which only asks reachability).

Follow-up questions

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

  • Jump Game (LC 55) — reachability only.
  • Jump Game V — bounded-step variants.
  • How does Snowflake minimize shuffle steps?

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 look only up to nums.length - 1?

Once you've reached or passed n-1, you're done. Continuing the loop would let you trigger a spurious extra jump.

Is this provably optimal?

Yes — it's BFS in disguise. Each iteration of the outer loop corresponds to one BFS level. Greedy reaches the same answer because the optimal substructure holds.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →