Jump Game solution explained

Jump Game is solved greedily: scan the array once while tracking the farthest index you could reach so far. If the scan ever arrives at an index beyond that farthest point, the end is unreachable; if it never does, you can get there. That runs in O(n) time with O(1) extra memory.

By Published 8 min read

The problem, reworded

You stand on the first square of a row of squares. Each square holds a whole number, the maximum distance you may jump forward from it. From a square holding 3 you may land 1, 2 or 3 squares ahead, never backward and never further. A 0 means you cannot leave that square. Return true if some sequence of jumps lands on the last square, false otherwise.

Arrays made up for this page, with answers from the code below
SquaresAnswerWhy
[2, 0, 2, 0, 1]trueJump 2 to index 2, then 2 more to the end
[3, 0, 0, 0, 2]falseIndex 3 is the farthest anyone gets, and it holds 0
[4, 0, 0, 0, 0]trueOne full-length hop clears every zero
[0]trueYou already stand on the last square
[2, 4, 1, 0, 1, 1]trueGo to index 1, then hop 4 to the end; greedy longest hops would get stuck
Arrays made up for this page, with answers from the code below. Scroll sideways on a small screen.

Ask whether the array can be empty (usually it has at least one square) and whether values can be negative (usually not). Also confirm that landing past the last square is not a thing: since you may choose any shorter hop, overshooting never matters, because a shorter hop that lands exactly on the end is always available.

What the interviewer is checking

Jump Game is short, and that is the point. It tests whether you can justify a greedy rule rather than just state it. Many candidates write the right loop from memory and then cannot explain why one number is enough. A clear answer moves through three steps: a correct but slow search, the observation that reachable squares always form an unbroken run from the start, and the one-pass loop that follows from it.

A tempting rule that fails

Always taking the longest hop sounds sensible and is wrong. On [2, 4, 1, 0, 1, 1], hopping 2 from index 0 lands on index 2, which allows only a hop of 1, onto index 3, which holds 0. Stuck. Yet stepping just 1 square to index 1 unlocks a hop of 4 that reaches the end. The size of a hop matters less than what the landing square offers next, so a rule that looks only at the current square cannot be trusted.

Slow but sure: mark good squares from the right

Call a square good if the end can be reached from it. The last square is good by definition. Walking leftward, a square is good if any square within its hop range is good. Square 0's label is the answer. Each square may scan up to its full hop range, so long hops make this O(n²).

def can_reach_end_slow(hops):
    good = [False] * len(hops)
    good[-1] = True
    for i in range(len(hops) - 2, -1, -1):
        for step in range(1, hops[i] + 1):
            if i + step < len(hops) and good[i + step]:
                good[i] = True
                break
    return good[0]
Right-to-left table: O(n²) time in the worst case, O(n) space.

The greedy: track the farthest reachable index

Here is the fact that removes the inner loop. If you can reach square k, you can reach every square before k too, because whatever jump landed on or past k could have been shortened. So the set of reachable squares is always one unbroken block starting at index 0, and a single number, its right edge, describes it completely.

Scan from left to right. At each index i, first ask whether i is inside the block. If i is beyond the farthest edge, there is a gap no jump can cross, so return false. Otherwise i is reachable, and from it you could go as far as i + hops[i], so stretch the edge if that is larger. If the scan finishes, every index, including the last, was inside the block.

def can_reach_end(hops):
    farthest = 0
    for i, hop in enumerate(hops):
        if i > farthest:
            return False  # this square is cut off
        farthest = max(farthest, i + hop)
    return True
Python: one pass, one number. O(n) time, O(1) extra space.
function canReachEnd(hops) {
  let farthest = 0;
  for (let i = 0; i < hops.length; i++) {
    if (i > farthest) return false;
    farthest = Math.max(farthest, i + hops[i]);
    if (farthest >= hops.length - 1) return true; // early exit
  }
  return true;
}
JavaScript: the same scan, with an early exit once the end is covered.

Trace on [3, 0, 0, 0, 2]

At index 0 the edge becomes 0 + 3 = 3. Indexes 1, 2 and 3 are inside the block, but each holds 0, so the edge stays at 3. Index 4 is beyond the edge, so the function returns false. The zero at index 3 is the wall: everything reachable ends there.

The same idea from the other end

Some people find a backward version easier to defend. Keep a goal, starting at the last index. Walk leftward, and whenever a square can hop onto or past the goal, move the goal to that square, since reaching it is now as good as reaching the end. At the end, the answer is whether the goal reached index 0. It is the right-to-left table with every row collapsed into one number.

def can_reach_end_backward(hops):
    goal = len(hops) - 1
    for i in range(len(hops) - 2, -1, -1):
        if i + hops[i] >= goal:
            goal = i
    return goal == 0
Python: moving the goal leftward. Also O(n) time and O(1) space.

Complexity

n squares
ApproachTimeExtra space
Try every path recursivelyExponential without a cacheO(n) call stack
Good-square table, right to leftO(n²) worst caseO(n)
Farthest reach, left to rightO(n)O(1)
Moving goal, right to leftO(n)O(1)
n squares. Scroll sideways on a small screen.

Edge cases

  • A single square: true, even if it holds 0, because you start on the end.
  • A 0 on the very first square of a longer array: false immediately, since the edge never passes index 0.
  • A 0 on the last square: harmless. You only need to land there, not leave.
  • Several zeros in a row: fine as long as some earlier square hops over all of them.
  • Very large hop values: the edge can exceed the array length. That is fine for a comparison, and the early exit in the JavaScript version stops the scan as soon as it happens.

Common mistakes

  • Checking reachability after updating the edge instead of before. Square i must be inside the block before its hop is allowed to count.
  • Returning false on the first zero you meet. A zero only matters when the edge stops on it.
  • Simulating the longest hop from each square, which fails on inputs like the last row of the examples.
  • Claiming the greedy is correct without the unbroken-block argument. That argument is the proof, so say it.

Follow-ups you may hear

  • Fewest jumps to the end. The edge idea turns into layers of squares reachable in one, two, three jumps. That is Jump Game II.
  • Jumps may go backward or forward. Reachable squares no longer form a single block, so switch to breadth-first search with a visited set.
  • Reach a square holding 0 from a given start, hopping exactly the value left or right. Again a graph search, since exact hops break the shorter-jump argument.
  • Return one valid path. Store, for each newly covered index, which square first reached it, then walk back from the end.

Saying it in the interview

Fictional example

How a candidate might walk through it
Candidate
If I can land on square k, I can land on anything before k, since I may always jump shorter. So the reachable squares form one run starting at zero.
Candidate
I only need the right edge of that run. I scan left to right, and if my index ever passes the edge, there's a gap I can't cross, so I return false.
Candidate
Otherwise I extend the edge with i plus the hop at i. One pass, constant memory. Taking the longest hop each time would be wrong, by the way; I can show a case.

Jump Game II asks for the fewest hops on the same kind of array. Gas Station is another greedy scan where one running number and one reset rule replace a quadratic search. When choices do conflict and greed stops working, a table is the fallback, as in Interleaving String. The coding problems hub lists them all.

Practicing with Natively

When you rehearse on Mac or Windows, Natively can read this problem from a screenshot and talk through why the reachable squares stay in one block, or suggest a case that breaks the longest-hop rule. It keeps a transcript of your mock and notes you can reread, which helps you tighten the proof into two sentences. See the AI interview assistant page for how it works.

Questions people ask

How do you solve Jump Game?

Scan left to right keeping the farthest index reachable so far. If the current index is beyond it, return false; otherwise extend it with the index plus its hop. If the scan finishes, the end is reachable.

Why is the greedy solution to Jump Game correct?

If you can reach a square, you can reach every square before it, because any jump can be shortened. So the reachable squares form one block from the start, and its right edge is all you need to track.

What is the time complexity of Jump Game?

The greedy is O(n) time and O(1) extra space. A table that checks every hop from every square is O(n²) in the worst case.

Does always taking the longest jump solve Jump Game?

No. A long hop can land on a square with a small or zero value while a shorter hop would reach a square with a big jump. Track the farthest reach instead of simulating one path.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP