Each number is the longest jump allowed from that position, and you win if you can get from the first position to the last. Walk left to right keeping one number, the farthest position reachable so far: reach = max(reach, i + jumps[i]). If you arrive at an index greater than reach, you can't be standing there, so the answer is false; if the loop finishes, it's true. That greedy pass is O(n) time and O(1) memory, and it replaces the O(n²) dynamic programming most people start with. The usual second question is the fewest jumps: the same walk, counted in rounds. All positions reachable with j jumps form a stretch; the farthest any of them can reach is the end of the stretch for j + 1 jumps. Count a jump each time the walk reaches the end of the current stretch.
"Can you win" — variant of LeetCode Jump Game I/II. Medium-level DSA, you should be able to solve 2 in 20-30 minutes.
Answer it out loud, in your own words. Then compare.
Interviewers listen for how you get there, not only where you land. The answer opens once you have tried.
reach is the farthest index any position seen so far can jump to. Positions are visited in order, so when i is greater than reach no earlier position gets to i, and none can get past it either. A zero is a wall only when reach stops exactly on it. Since a jump may be shorter than its number, reaching index r means every index up to r is reachable, which is why one number can stand for the whole set.
// jumps[i] is the longest jump allowed from position i. You start at position 0; you win if// you can reach the last position. Track the farthest position reachable so far: if the walk// ever arrives at a position beyond it, that position (and everything after) is out of reach.function canWin(jumps) {let reach = 0;for (let i = 0; i < jumps.length; i++) {if (i > reach) return false;reach = Math.max(reach, i + jumps[i]);}return true;}
passes 7 checks run on every build, against the native method where there is one
- the two classic examples
- one position is already the end
- a zero is a wall only if nothing jumps over it
- landing exactly on the end, and far past it
- a jump is "up to", not "exactly"
- agrees with the definition on 300 small boards
- a long board in one pass
end marks the last position reachable with count jumps. While walking towards it, reach records the best that one more jump could do from any position in this round. On arriving at end, a jump is spent and the next round runs up to reach. The loop stops before the last index because standing on it needs no further jump. If a round ends without reach moving past it, the end is unreachable and the answer is -1.
// The second question: the fewest jumps to the end, or -1 if it can't be reached.// Think in rounds. `end` is the last position reachable with the jumps counted so far; while// walking up to it, `reach` collects how far one more jump could get. Arriving at `end` means// another jump has to be spent.function minJumps(jumps) {let count = 0;let end = 0;let reach = 0;for (let i = 0; i < jumps.length - 1; i++) {reach = Math.max(reach, i + jumps[i]);if (i === end) {if (reach === i) return -1; // nothing in this round gets past herecount++;end = reach;}}return count;}
passes 7 checks run on every build, against the native method where there is one
- the classic examples
- already at the end: no jumps
- unreachable is -1
- one big jump, or many small ones
- the greedy choice is the position that reaches farthest, not the longest jump
- agrees with the definition on 300 small boards
- a long board in one pass
To print the positions, record for each one the earliest position that can jump onto it. The outer loop visits positions in order; the inner loop only assigns parents to positions that don't have one yet, so every position is written once and the whole thing stays O(n). Because positions are discovered in order of jump count, following the parents back from the last position gives a route with the fewest jumps. The checks compare the route's length with a slow dynamic-programming count on 300 boards and verify that every hop is legal.
// The follow-up: which positions do you land on? Give every position a parent, the earliest// position that can jump to it. Positions are reached in order of jump count, so the earliest// parent is on a shortest route. Each position gets its parent once: still one pass.function jumpPath(jumps) {const n = jumps.length;if (n === 0) return [];const from = new Array(n).fill(-1);let reached = 0; // the highest position that already has a parentfor (let i = 0; i < n && i <= reached; i++) {const far = Math.min(n - 1, i + jumps[i]);for (let j = reached + 1; j <= far; j++) from[j] = i;reached = Math.max(reached, far);}if (reached < n - 1) return null;const path = [];for (let at = n - 1; at !== -1; at = from[at]) path.push(at);return path.reverse();}
passes 6 checks run on every build, against the native method where there is one
- the positions landed on, start and end included
- already at the end
- unreachable is null
- it takes the route with the fewest jumps
- every route is legal and shortest, on 300 small boards
- a long board in one pass
Greedy instead of dynamic programming
The reachable positions always form one unbroken stretch from the start, so the whole state of the search is a single number. Spotting that a problem's state collapses like this is what turns an O(n²) table into an O(n) pass.
It is breadth-first search in disguise
The rounds of the second stage are the levels of a breadth-first search over positions, where each position links to the next few. Shortest path in an unweighted graph is always BFS; here the levels happen to be contiguous ranges, so no queue is needed.
Hop limits in routing and pagination
"Can I get from here to there in at most k hops, each of limited length" appears in network routing (hop counts) and in planning refuel or sync points along a route. The fewest-stops version is this problem.
Reading the number as an exact jump length instead of a maximum.
Trying every jump from every position recursively without memoising: exponential time.
Treating any zero as a dead end, when a longer earlier jump can pass over it.
In the fewest-jumps version, always taking the longest jump instead of the position that reaches farthest next.
Counting one jump too many by running the loop over the last index.
Not saying what to return when the end can't be reached.
Why is the greedy answer correct?
By induction on jumps: the positions reachable with at most j jumps are exactly 0 to some index e_j, because any shorter jump is also allowed. The positions reachable with j + 1 jumps are then 0 to the largest i + jumps[i] over i ≤ e_j. Taking that maximum each round loses nothing.
What is the dynamic programming solution, and why not use it?
best[j] = min(best[i] + 1) over every i that can reach j. It is correct and O(n²). It is the version used in the checks to prove the greedy one. On arrays of 10⁵ positions it is too slow.
What if jumps can also go backwards?
Then reachable positions no longer form one stretch and the single number is not enough. Use a real breadth-first search with a visited array; it is O(n + edges).
What if it is a two-player game where players alternate jumps?
That is a different problem: mark each position as winning or losing for the player to move, working backwards from the end. A position is winning if some jump from it lands on a losing position.