91. Jump Game II
mediumAsked at SnowflakeFind 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 Sam K., 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^40 <= nums[i] <= 1000It's guaranteed that you can reach nums[n - 1].
Examples
Example 1
nums = [2,3,1,1,4]2Example 2
nums = [2,3,0,1,4]2Approaches
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.
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.
More Snowflake coding interview questions
- 1. Two Sum
- 2. Valid Parentheses
- 3. Merge Two Sorted Lists
- 4. Remove Duplicates from Sorted Array
- 5. Remove Element
- 6. Search Insert Position
- 7. Plus One
- 8. Merge Sorted Array