Given an array of non-negative integers where each element represents your maximum jump length from that position, determine if you can reach the last index starting from the first index.
For example, given [2, 3, 1, 1, 4], return true (jump 1 step from index 0 to 1, then 3 steps to the end). Given [3, 2, 1, 0, 4], return false (you always end up stuck at index 3).
Explain your approach to solving this problem and state the time and space complexity.
How to approach it
- Hint 1
Think about the farthest position you can reach as you iterate through the array.
- Hint 2
At each index, if you can reach it (index <= farthest), update the farthest reachable position.
- Hint 3
Maintain a variable maxReach. For each index i, if i > maxReach you are stuck. Otherwise, maxReach = max(maxReach, i + nums[i]). If maxReach >= last index, return true.
Ready to answer it out loud?
Record your answer in 1:30 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.
Practising needs an invite code. Join the waitlist and we’ll send you one.