DSA Tracker

Problem

Hard

Frog Jump

A hard Dynamic Programming interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Dynamic Programming
Sheets
1
Core for
9 roles

The problem

Stones sit at the given positions along a river, in increasing order, with the first stone at position 0. A frog on the first stone must make its first jump exactly 1 unit, and after a jump of k units its next jump must be k - 1, k or k + 1 units, always forward and always onto a stone. Decide whether the frog can land on the last stone.

Example 1

Input
stones = [0,1,3,6,10,15]
Output
true
Why
Jumps: 1,2,3,4,5. Each jump increases by 1, reaching the last stone.

Example 2

Input
stones = [0,2,3,4,5,6,7,8]
Output
false
Why
First jump must be 1, but stone 1 does not exist. The frog cannot move from the start.

Example 3

Input
stones = [0,1]
Output
true
Why
The frog jumps 1 unit to the last stone.

Constraints

  • 2 <= stones.length <= 2000
  • 0 <= stones[i] <= 2^31 - 1
  • stones[0] == 0
  • stones is sorted in ascending order

How to think about it

Updated 2026-09-29

The state at any stone depends on how fast the frog was moving when it landed: stone position alone is not enough, but the incoming jump distance k is. Associate with each stone the set of jump sizes that can reach it, and propagate forward jumps of size k-1, k, and k+1 to stones matching position + step.

Approaches, worst first

  1. Depth-first search with position and jump

    time O(3^n) · space O(n^2)

    From stone i with incoming jump k, branch into k-1, k, k+1. Without caching visited (index, k) pairs, routes crossing stones in different orders duplicate exponential paths.

  2. DP over reachable jump setsWrite this one

    time O(n^2) · space O(n^2)

    Store a map mapping stone position to a set of possible incoming jump lengths. For each stone pos, iterate its incoming jumps k: for step in [k-1, k, k+1], if step > 0 and pos + step exists in the map, add step to the destination stone's jump set.

Where people lose marks · 3
  • First jump must be exactly 1 unit to stone 1: if stones[1] != 1, the frog cannot even make its first move.
  • Allowing jump sizes <= 0: if k - 1 == 0, attempting a jump of 0 keeps the frog stuck in place indefinitely.
  • Using a dense 2D array indexed by stone positions. Stone coordinates can reach 2^31 - 1, which will exhaust memory unless coordinates are indexed by array indices or stored in hash structures.

The theory behind it

Dynamic Programming — the ground this problem stands on. All Dynamic Programming problems

What Dynamic Programming is

Dynamic programming is a method for solving a complex problem by breaking it into overlapping subproblems, solving each subproblem only once, and remembering the answers in a lookup table. Instead of recalculating identical questions over and over, future steps look up previous answers directly. By assembling these saved pieces from the bottom up or storing them during recursion, a task that would take billions of steps finishes in a fraction of a second.

When to reach for it

Reach for dynamic programming when questions ask for the maximum profit, minimum cost, total number of distinct ways to achieve a goal, or whether a target can be formed. Signals include overlapping choices where making a choice now affects what choices remain later, but greedy picking fails to find the true global optimum. If drawing a recursive decision tree reveals the same subproblem states repeating across branches, dynamic programming is needed.

How the pattern works

Identify the state variables that uniquely describe a subproblem, such as an array index and remaining capacity. Write the base cases first, representing states whose answers are known without calculation. Next, write the recurrence relation that expresses the current state using previously solved states, taking the minimum, maximum, or sum among your options. Build the solution either top-down by caching recursive returns in a memo table, or bottom-up by filling an array in topological dependency order. When each state depends only on the previous row, compress storage down to a single array.

More Dynamic Programming problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .