InterviewPrepKit

Home / Coding / Algorithm & Data Structure / Greedy

Jump Game

medium Original ↗ 00:00

Problem

You are given an integer array nums. You start at index 0. Each nums[i] is the maximum jump length from index i — from i you may step to any index in i+1, i+2, ..., i+nums[i] (as long as it stays in bounds). Return True if you can reach the last index, False otherwise.

Examples

  • nums = [2,3,1,1,4]True — jump 0→1 (length 1), then 1→4 (length 3), landing on the last index.
  • nums = [3,2,1,0,4]False — every route stalls at index 3, whose value 0 allows no further jump; index 4 is unreachable.
  • nums = [0]True — you are already on the last (and only) index.

Constraints

  • 1 <= len(nums) <= 10^4
  • 0 <= nums[i] <= 10^5

An O(n) scan is expected. The 0 values are what make the problem non-trivial, since a 0 blocks any further jump from that index.

Think about it first

Hint 1 You do not need to know how you get somewhere, only whether it is reachable. As you move left to right, what single number summarizes everything reachable so far?
Hint 2 Track the farthest index reachable. At each position i, first check: is i itself within the current reach? If not, you are stuck and can never proceed.
Hint 3 If i is reachable, update the frontier to max(farthest, i + nums[i]). If the frontier ever reaches or passes the last index, return True.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug