Jump Game

medium

You are given an integer array nums. You start at the first index, and each nums[i] is the maximum length of a forward jump from index i.

Return true if you can reach the last index, and false otherwise.

Hints

You don't need the exact path — just how far you can possibly reach.
Sweep left to right, keeping the farthest index reachable so far.
If an index lies beyond that farthest reach, you can never arrive — return false.

Common doubts

If the farthest reachable index covers position i, then i is reachable; the specific route doesn't matter, only whether the frontier extends to the end.
It's the only failure condition: if the current index is past everything you could reach, no earlier jump lands here, so the end is unreachable.
No — a zero only traps you if the frontier doesn't already extend beyond it. If an earlier jump reaches past the zero, it's harmless.

Interview follow-ups

Extend the greedy: track the current jump's frontier and the next reachable frontier, incrementing a jump count when you exhaust the current one.
Remember, for each frontier extension, which index produced it; then walk those choices back from the end.

Fun facts

  • This 'farthest reachable' frontier is a one-dimensional version of a BFS wavefront — you're implicitly doing breadth-first reachability in O(1) space.
  • The same running-maximum-of-reach idea underlies interval covering and gas-station problems.

Asked at

AmazonGoogleMicrosoftMeta
Frequently Sometimes Occasionally
Example 1
Input: nums = [2,3,1,1,4]
Output: true
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2
Input: nums = [3,2,1,0,4]
Output: false
You always arrive at index 3 (value 0), which you can't move past to reach the end.
Constraints

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

Solve this problem →