55. Jump Game
Asked at Oracle, Bloomberg
Difficulty
Medium
Time
O(n)
Space
O(1)
Pattern
Array
Companies
2
Problem
You get an array of non-negative integers called nums. You start standing on index 0. The value at each index tells you the maximum number of steps you may jump forward from that position. You can jump any distance from 1 up to that value, and you may not jump backward.
Return true if some sequence of jumps lets you land on the last index of the array. Otherwise return false.
A value of 0 means you are stuck at that index — you can only jump 0 steps, so you cannot move forward from there.
How far can you jump from each index
Constraints
- ●
1 <= nums.length <= 10^4 - ●
0 <= nums[i] <= 10^5
Examples
nums = [2,3,1,1,4]truenums = [3,2,1,0,4]falseAsked At
2 companies| Company | Difficulty | |
|---|---|---|
| Oracle | Medium | View all Oracle questions → |
| Bloomberg | Medium | View all Bloomberg questions → |
How to Think About It
Think about what each index value means — it tells you how far you can jump from that position.
A brute force approach tries every possible jump from each index using recursion, leading to exponential time.
Dynamic programming can optimize by storing whether each index is reachable, but this still requires O(n) space.
A greedy approach tracks the farthest reachable index as you iterate through the array.
If at any point the current index exceeds the farthest reachable index, return false. Otherwise, return true.
Optimal Approach
Iterate through the array while maintaining the farthest index you can reach. For each position i, update the farthest reachable index as max(farthest, i + nums[i]). If at any point the current index i is beyond the farthest reachable index, you cannot proceed and return false. If you complete the loop, the last index is reachable. This greedy approach runs in O(n) time and O(1) space.
Track the farthest reachable index
What Trips People Up in Real Interviews
Clarify whether you can jump exactly or at most the given number of positions.
Start with the brute force recursion to show problem understanding before optimizing.
Explain the greedy insight clearly — you only need to track the maximum reachable index.
Mention that this can also be solved with DP but greedy is optimal at O(n) time and O(1) space.
Edge cases: single element array (always true), first element is 0 and array has more than one element (false).
Solution Code
O(n) timedef canJump(nums):
farthest = 0
for i in range(len(nums)):
if i > farthest:
return False
farthest = max(farthest, i + nums[i])
return TrueFrequently Asked Questions
What is the Jump Game problem?
You are given an integer array nums where each element represents the maximum number of positions you can jump forward from that index. Determine whether you can reach the last index starting from the first index.
How do you solve Jump Game?
The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Jump Game?
Jump Game is asked at Oracle, Bloomberg. It is a medium difficulty problem.
What are common mistakes on Jump Game?
- Clarify whether you can jump exactly or at most the given number of positions.
- Start with the brute force recursion to show problem understanding before optimizing.
- Explain the greedy insight clearly — you only need to track the maximum reachable index.
- Mention that this can also be solved with DP but greedy is optimal at O(n) time and O(1) space.
- Edge cases: single element array (always true), first element is 0 and array has more than one element (false).