Jump Game VII solution explained

Jump Game VII gives you a string of 0s and 1s and a jump range from lo to hi; starting on index 0, you may land only on a 0, and the question is whether you can land on the last index. The efficient answer walks the string once while keeping a count of landable indexes inside the window that can reach the current position, so each index is decided in O(1). Total cost: O(n) time and O(n) space.

By Published 7 min read

The task, restated

Picture a row of tiles written as a string. A '0' is a safe tile and a '1' is a hole. You stand on tile 0, which is always safe. On each move you jump forward by at least lo and at most hi tiles, and you must land on a safe tile that is still inside the row. Return true if some sequence of jumps lands you exactly on the last tile, and false otherwise.

Our own test strings
TileslohiAnswerWhy
011010023true0 to 3 (index 2 is a hole), then 3 to 6.
00011022falseExact jumps of 2 go 0, 2, 4, and tile 4 is a hole.
010010033trueTwo jumps of exactly 3.
0011011024true0 to 4, then 4 to 7.
000113falseThe last tile is a hole, so no route can end there.
Our own test strings. Scroll sideways on a small screen.

Clarify early: can lo equal hi (yes, and then every jump has a fixed length), is the first character guaranteed to be '0' (normally yes), and how long can the string be? Expect a long string; if the interviewer confirms it, that is the hint a quadratic scan will be too slow.

Why the obvious search is too slow

The direct approach mirrors the rules. Keep a boolean per tile. Mark tile 0. Walk left to right, and from each marked tile mark every safe tile in its jump range. When the walk ends, read the last tile's mark.

def can_finish_slow(cells, lo, hi):
    n = len(cells)
    ok = [False] * n
    ok[0] = True
    for i in range(n):
        if not ok[i]:
            continue
        for j in range(i + lo, min(i + hi, n - 1) + 1):
            if cells[j] == "0":
                ok[j] = True
    return ok[n - 1]
Marking every target: correct, O(n * (hi - lo + 1)) time.

Many reachable tiles have overlapping ranges, so the same tile gets marked again and again. With a wide window and a long string of mostly zeros, the inner loop runs close to n times per tile and the total approaches O(n²). It is correct, and it makes a good brute-force reference, but it will not finish on large inputs.

Flip the question around

Instead of asking where each tile can send you, ask which tiles could have sent you here. Tile j can be reached from tile i exactly when j - hi ≤ i ≤ j - lo. So j is reachable if it is safe and at least one reachable tile sits in that window. The window moves right by one each time j does: one tile enters on the right edge (index j - lo) and one leaves on the left edge (index j - hi - 1). Track how many reachable tiles are inside it, and each decision becomes constant time.

def can_finish(cells, lo, hi):
    n = len(cells)
    ok = [False] * n
    ok[0] = True
    live = 0  # reachable indexes inside [j - hi, j - lo]
    for j in range(1, n):
        if j - lo >= 0 and ok[j - lo]:
            live += 1
        if j - hi - 1 >= 0 and ok[j - hi - 1]:
            live -= 1
        ok[j] = cells[j] == "0" and live > 0
    return ok[n - 1]
Python: sliding count of reachable tiles, O(n) time and O(n) space.
function canFinish(cells, lo, hi) {
  const n = cells.length;
  const ok = new Array(n).fill(false);
  ok[0] = true;
  let live = 0;
  for (let j = 1; j < n; j++) {
    if (j - lo >= 0 && ok[j - lo]) live++;
    if (j - hi - 1 >= 0 && ok[j - hi - 1]) live--;
    ok[j] = cells[j] === "0" && live > 0;
  }
  return ok[n - 1];
}
JavaScript: the same window, with the count updated as it slides.

The order of the two updates matters less than the boundaries. The entering index is j - lo because that is the furthest-right tile that can still reach j. The leaving index is j - hi - 1 because once j advances, that tile is now more than hi steps behind and can no longer reach it. Off-by-one errors here are the most common bug, so trace a tiny case before you claim it works.

Trace: 0110100 with lo = 2 and hi = 3

How the count moves at each tile
jEnters (j - 2)Leaves (j - 4)liveTileReachable
1nonenone01no
2tile 0, reachablenone11no (hole)
3tile 1, not reachablenone10yes
4tile 2, not reachabletile 0, reachable01no
5tile 3, reachabletile 1, not reachable10yes
6tile 4, not reachabletile 2, not reachable10yes
How the count moves at each tile. Scroll sideways on a small screen.

Tile 6 is reachable, so the answer is true. Note that tile 5 is reachable too, from tile 3, even though the route in the examples table jumped from 3 straight to 6. The algorithm records every landable tile, not one path.

A second linear answer

Some candidates prefer breadth-first search. Put tile 0 in a queue and keep a pointer to the furthest index already examined. When you pop tile i, scan only from max(i + lo, pointer + 1) up to min(i + hi, n - 1), enqueue every safe tile, then move the pointer to i + hi. Because the pointer never moves backward, each tile is scanned once overall, which is also O(n). Both answers are fine; the sliding count is shorter to write and has no queue.

Complexity side by side

n is the string length and w = hi - lo + 1 is the window width
MethodRunning timeMemoryVerdict
Mark every targetO(n · w)O(n)Reference only
Sliding countO(n)O(n) for the reachable flagsPreferred
BFS with a scan pointerO(n)O(n) for the queueEqually fast, more code
Prefix sums over reachable flagsO(n)O(n)Same idea, stated as range sums
n is the string length and w = hi - lo + 1 is the window width. Scroll sideways on a small screen.

Edge cases worth testing

  • The last character is '1': the answer is false no matter what, and you can return early.
  • A string of length 1: you already stand on the last tile, so the answer is true.
  • lo equal to hi: every jump has the same length, and the window holds a single tile.
  • hi larger than the string: the window simply clips at index 0, which the bounds checks handle.
  • A long run of '1's wider than hi: nothing on the far side is reachable, and live drops to zero inside the run.

Mistakes interviewers see

  • Window edges off by one. Adding j - lo + 1 or removing j - hi breaks cases where lo equals hi.
  • Counting safe tiles instead of reachable ones. The window must count tiles you can stand on, not every '0'.
  • Greedy longest jump. Always jumping hi can land in a hole when a shorter jump would have worked. With 00010, lo = 1 and hi = 3, a jump of 3 hits the hole at index 3, while 0 to 1 to 4 succeeds.
  • Recursion with memo on a long string. It is correct in principle, but deep recursion and repeated range scans bring back the slow bound.
  • Forgetting that tile 0 counts. It must be marked reachable before the loop starts.

Saying it out loud

Fictional example

How a candidate might pitch the window
Candidate
Pushing reachability forward costs the window width per tile. I would rather pull: tile j is reachable if it is a '0' and some reachable tile lies between j minus hi and j minus lo.
Candidate
That range slides by one each step, so I keep a running count of reachable tiles in it. Add the tile at j minus lo, drop the tile at j minus hi minus one.
Candidate
One pass, constant work per tile, linear memory for the flags.

Jump Game III is the two-direction version where a visited set does the work. Frog Jump changes the rule so the allowed range depends on your previous jump, which turns the state into a pair. If sliding windows are new to you, the patterns in data structure interview questions are a good warm-up.

Practice with Natively

In a practice session, Natively can capture a problem statement with a screenshot and propose an approach, so you can see whether it reaches for the sliding window or the BFS pointer and compare that with your own plan. It also keeps a transcript of mock interviews, which is a quick way to notice whether you explained the window boundaries or skipped them. See an AI interview assistant.

Questions people ask

What is the optimal solution to Jump Game VII?

Walk the string once and keep a count of reachable indexes inside the window [j - hi, j - lo]. Index j is reachable when it is '0' and that count is above zero. This runs in O(n) time.

Why is the simple approach too slow for Jump Game VII?

Marking every index in each reachable tile's range repeats work across overlapping windows, which costs O(n times the window width) and can approach O(n²).

Can Jump Game VII be solved with BFS?

Yes. Use a queue plus a pointer to the furthest index already scanned, so each index is examined once. It is also O(n).

Which window boundaries are easy to get wrong?

The index entering the window is j - lo and the one leaving is j - hi - 1. Shifting either by one breaks cases where lo equals hi.

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