Question presented to candidate: "You are given a list of non-negative integers. You start at index 0, and each value is the maximum jump length from that position. Write a Python function that returns whether you can reach the last index."
What a strong answer should cover:
- The key observation: shorter jumps are allowed, so the reachable indices always form a prefix, and one number โ the farthest reach โ describes them.
- The one-pass greedy: return false as soon as
i > farthest, otherwise updatefarthest = max(farthest, i + nums[i]); O(n) time, O(1) space. - A correctness argument (the prefix / loop-invariant argument), not just the code.
- The slower alternatives and why they lose: recursion (exponential) and the
good[]DP (O(nยฒ)). - Python details:
enumerate(nums)for index and value together, an earlyreturn Trueonce the end is covered, and avoiding recursion because of the default recursion limit. - The Jump Game II follow-up (fewest jumps) as BFS levels.
Clarifying questions expected:
- Is each value the maximum jump (shorter jumps allowed)?
- Do you want only yes/no, or the fewest jumps?
Code / implementation expected: Yes โ can_jump(nums) -> bool in Python 3, one pass.