Frog Jump solution explained

Frog Jump asks whether a frog can hop from the first stone to the last when its first hop is exactly 1 unit and every later hop must be one shorter, the same, or one longer than the hop before. The standard answer records, for every stone, the set of hop lengths that can land on it, and grows those sets stone by stone. That takes O(n²) time and space for n stones, versus exponential time for plain recursion.

By Published 8 min read

The rules in plain terms

A river has stones at whole-number positions, given to you as a sorted list that starts at 0. A frog sits on the first stone. Its first hop must cover exactly 1 unit. After a hop of length k, the next hop can be k - 1, k or k + 1 units long, must be positive, and must land on a stone; water is fatal. Hops only go forward. Return true if the frog can land on the last stone.

Stone lists made up for this walkthrough
StonesResultWhat happens
[0, 1, 2, 4, 7, 11]trueHops of 1, 1, 2, 3, 4: each grows by at most one.
[0, 1, 2, 3, 6]trueStone 3 can be reached by a hop of 1 or of 2, and only arriving with 2 allows the hop of 3 to stone 6.
[0, 1, 3, 4, 8]falseThe frog reaches 4, but from there no hop of allowed length finds a stone.
[0, 2]falseThe first hop must be 1, and there is no stone at 1.
[0, 1, 3, 6, 10, 15, 16]falseHops 1, 2, 3, 4, 5 work, but the last hop would need length 1 after a 5.
Stone lists made up for this walkthrough. Scroll sideways on a small screen.

The last row is a good reminder that hops can also be too long. After building speed, the frog cannot suddenly take a tiny step. That is what makes the problem stateful: whether a stone helps depends on how you arrived.

Why position alone fails

A tempting first idea is a boolean per stone saying whether it is reachable. It does not work. In the second example, stone 3 is reachable two ways, by a hop of 1 from stone 2 and by a hop of 2 from stone 1. The next stone is 3 units away. A frog that arrived with a hop of 2 may hop 3, but one that arrived with a hop of 1 can manage at most 2. If you record only that stone 3 was reached, you lose the information that decides the answer. The real state is the pair (stone, size of the hop that landed there).

Slow approach: try every route

Recursion follows the rules directly. From a stone with previous hop k, try the three hop sizes, and recurse on each one that lands on a stone. Return true as soon as any branch reaches the end.

def frog_slow(stones):
    where = set(stones)
    goal = stones[-1]

    def go(pos, last):
        if pos == goal:
            return True
        for step in (last - 1, last, last + 1):
            if step > 0 and pos + step in where and go(pos + step, step):
                return True
        return False

    return go(stones[0], 0)
Plain recursion: correct, but the number of routes can grow exponentially.

Starting with a previous hop of 0 is a small trick: the three candidates become -1, 0 and 1, and only 1 is positive, which enforces the rule about the first hop without special code. The weakness is that the same (stone, hop) pair is reached by many different routes and explored again each time. Adding a memo on that pair fixes it and gives the same bounds as the approach below.

Main approach: hop-size sets per stone

Build a dictionary from each stone's position to an empty set. Put 0 in the first stone's set, meaning the frog arrived there with a hop of zero. Then visit the stones from left to right. For each hop size k already in the current stone's set, try k - 1, k and k + 1; whenever the landing spot is a stone, add that hop size to its set. Because hops only go forward, a stone's set is complete by the time you reach it. At the end, the answer is whether the last stone's set is non-empty.

def frog_crosses(stones):
    arrivals = {s: set() for s in stones}
    arrivals[stones[0]].add(0)
    for s in stones:
        for k in arrivals[s]:
            for step in (k - 1, k, k + 1):
                if step > 0 and s + step in arrivals:
                    arrivals[s + step].add(step)
    return len(arrivals[stones[-1]]) > 0
Python: forward DP over hop-size sets, O(n²) time and space.
function frogCrosses(stones) {
  const arrivals = new Map();
  for (const s of stones) arrivals.set(s, new Set());
  arrivals.get(stones[0]).add(0);
  for (const s of stones) {
    for (const k of arrivals.get(s)) {
      for (const step of [k - 1, k, k + 1]) {
        if (step > 0 && arrivals.has(s + step)) arrivals.get(s + step).add(step);
      }
    }
  }
  return arrivals.get(stones[stones.length - 1]).size > 0;
}
JavaScript: a Map of Sets, filled in stone order.

The sets for [0, 1, 2, 3, 6]

Hop sizes recorded at each stone after the pass
StoneArrival hopsWhere they lead
0{0}Only a hop of 1 is positive, to stone 1
1{1}Hop 1 to stone 2, hop 2 to stone 3
2{1}Hop 1 to stone 3; hop 2 would hit water at 4
3{1, 2}From hop 1, hops of up to 2 find only water; from hop 2, a hop of 3 reaches 6
6{3}Last stone reached, answer true
Hop sizes recorded at each stone after the pass. Scroll sideways on a small screen.

How expensive is it

The hop that lands on the i-th stone (counting from 0) is at most i units long, since the first hop is 1 and each hop grows by at most one while moving at least one stone forward. So each set holds at most n values, and the total number of (stone, hop) pairs is O(n²). Each pair does constant work with hash lookups, which gives O(n²) time and O(n²) memory in the worst case. Typical inputs fill far fewer pairs.

Approaches compared for n stones
ApproachTimeSpaceNotes
Unmemoized recursionExponential in the worst caseO(n) call stackUseful only as a test oracle
Recursion with a memo on (index, hop)O(n²)O(n²) memo plus stackDeep stacks in Python on long inputs
Forward sets per stoneO(n²)O(n²)Iterative and short
2D table dp[i][k] over indexesO(n²)O(n²)Same idea with arrays; needs a position-to-index map
Approaches compared for n stones. Scroll sideways on a small screen.

The bound on hop length also gives a free early exit. If the gap between stone i - 1 and stone i is larger than i, no hop that could exist at that point spans it, so return false before doing any DP. It does not change the worst case but saves time on hopeless inputs, and mentioning it shows you understand why the sets stay small.

Edge cases

  • Second stone not at position 1: the answer is false immediately.
  • Only one stone: the frog is already there, so the answer is true if the input allows it.
  • Two stones at 0 and 1: true with a single hop.
  • A wide gap: near the start it is fatal, while further along the frog may have built up enough hop length to clear it.
  • Very large coordinates: positions are looked up in a hash map, so size of the numbers does not matter, only the count of stones.

Common mistakes

  • Tracking reachability per stone only. It throws away the hop length, which decides the next move.
  • Allowing a hop of 0. That would let the frog stand still and can make recursion loop. Hop sizes must stay positive.
  • Forgetting the first-hop rule. Some solutions let the first hop be any length; seeding the first stone with 0 fixes that cleanly.
  • Indexing a 2D table by position. Positions can be huge. Index by stone number and use a dictionary from position to index.
  • Modifying a set while iterating it. In this forward version the landing stone is always further right, so the set being read is never the one being written. A backward or in-place variant can break that.

Walking an interviewer through it

Fictional example

Explaining the state choice
Candidate
Where the frog is does not tell me what it can do next. I also need the size of the last hop, so my state is the pair.
Candidate
I will keep a set of arrival hop sizes per stone. Seed the first stone with zero so the only legal first hop is one, then sweep left to right and push k minus one, k and k plus one forward.
Interviewer
Why is that quadratic and not worse?
Candidate
Each hop grows by at most one per stone, so the hop landing on stone i is at most i. That caps each set at n entries.

Other walkthroughs in this group

Jump Game VII is the cousin with a fixed jump range, where a sliding count replaces the per-stone sets. Minimum Jumps to Reach Home also needs a pair as its state, this time position plus the direction of the last jump, and asks for the fewest moves. For more practice choosing state, read algorithm interview questions.

Where Natively fits in your prep

Dynamic programming problems are easier to learn when you can check your state choice quickly. While practicing, Natively can read the problem from a screenshot and outline an approach and code, so you can compare its state definition with yours once you have tried. Recording a mock round with its transcript lets you check later whether you justified the O(n²) bound or skipped it. Details on an AI interview assistant.

Questions people ask

How do you solve the Frog Jump problem?

Map each stone to the set of hop sizes that can land on it. Seed the first stone with 0, sweep the stones in order, and for each hop size k add k - 1, k and k + 1 to the stones they land on. The frog crosses if the last stone's set is not empty.

What is the time complexity of Frog Jump?

O(n²) time and space for n stones, because the hop landing on the i-th stone is at most i units long, so each stone holds at most n hop sizes.

Why is a reachable flag per stone not enough for Frog Jump?

The next hop depends on the previous one. The same stone reached with different hop sizes can lead to different futures, so the state must include the hop size.

Is Frog Jump dynamic programming or BFS?

It is usually taught as dynamic programming over (stone, last hop). A memoized DFS or a BFS over the same pairs gives the same O(n²) bound.

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