DSA Tracker

Problem

Pattern this teaches:Greedy Scheduling and Sorting

Medium

Jump Game

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

Topic
Dynamic Programming
Sheets
3
Core for
9 roles

The problem

Given an array of non-negative integers where each element represents your maximum jump length at that position, determine if you can reach the last index starting from the first index.

Example 1

Input
nums = [3,2,2,0,4]
Output
true
Why
Jump 2 steps to index 2 (value 2), then 2 steps to the last index.

Example 2

Input
nums = [2,1,0,3,0]
Output
false
Why
From index 0 you can reach index 2 (value 0) but then cannot move further. The last index is unreachable.

Example 3

Input
nums = [0]
Output
true
Why
You are already at the last index.

Constraints

  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= 10^5

How to think about it

Updated 2026-09-29

The exact sequence of jumps taken to reach any point is irrelevant; all that matters is the boundary of the furthest index reachable so far. As you walk forward, if the current index ever exceeds that boundary, you are stranded and can proceed no further.

Approaches, worst first

  1. Recursive jump simulation

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

    From current index i, try every jump length from 1 to nums[i]. Explores duplicate suffix paths repeatedly, leading to exponential time.

  2. Dynamic programming reachability array

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

    Maintain a boolean array dp where dp[i] indicates whether index i is reachable. For each reachable i, mark all indices up to min(n-1, i + nums[i]) as true. Takes quadratic time.

  3. Greedy max reach sweepWrite this one

    time O(n) · space O(1)

    Track an integer `maxReach`. Iterate through indices: if i > maxReach, return false. Otherwise, update maxReach = max(maxReach, i + nums[i]). If maxReach >= n - 1, return true.

Where people lose marks · 3
  • Checking whether i > maxReach after updating maxReach with nums[i], which lets you jump from an index you could never reach in the first place.
  • Failing on a single-element array where nums = [0]. You are already at the last index, so the answer must be true.
  • Treating any encountered 0 as an automatic failure. A zero only halts progress if the furthest reach cannot bypass it.

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 .