Determine reachability and minimum jumps
Company: SoFi
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Determine reachability and minimum jumps states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Jump Game: Reachability
Constraints
- 1 <= n <= 10^4 (problem also defines the empty array as trivially reachable)
- 0 <= nums[i] <= 10^5
- All values are non-negative integers
Examples
Input: ([2, 3, 1, 1, 4],)
Expected Output: True
Explanation: From index 0 (value 2) jump to index 1, which can reach index 4. Reachable.
Input: ([3, 2, 1, 0, 4],)
Expected Output: False
Explanation: The best you can do lands on index 3 (value 0), which blocks all progress; index 4 is unreachable.
Input: ([0],)
Expected Output: True
Explanation: n == 1: you start already on the last index, so it is trivially reachable even though nums[0] == 0.
Input: ([],)
Expected Output: True
Explanation: Empty array edge case: defined as trivially reachable (no last index to fall short of).
Input: ([1, 0, 1],)
Expected Output: False
Explanation: Index 0 reaches only index 1 (value 0), which is a dead end before index 2.
Input: ([2, 0, 0],)
Expected Output: True
Explanation: Index 0 (value 2) jumps straight over the zeros to the last index.
Input: ([5, 0, 0, 0, 0, 0],)
Expected Output: True
Explanation: A single very large jump from index 0 clears all trailing zeros to reach index 5.
Input: ([0, 1, 2],)
Expected Output: False
Explanation: Index 0 has value 0, so you are stranded at the start and cannot move at all.
Hints
- You do not need to track every path. Track a single number: the farthest index reachable so far.
- Scan left to right. If the current index i ever exceeds the farthest reachable index, you are stranded behind a wall of zeros and can never reach the end.
- At each index update farthest = max(farthest, i + nums[i]); as soon as farthest >= n-1 you can stop early and return true.
- Edge cases: n == 1 (already at the last index, return true) and a leading or interior run of zeros that the farthest reach cannot clear.
Jump Game II: Minimum Jumps
Constraints
- 1 <= n <= 10^4 (problem also defines the empty array as needing 0 jumps)
- 0 <= nums[i] <= 10^5
- All values are non-negative integers; return -1 when the last index cannot be reached
Examples
Input: ([2, 3, 1, 1, 4],)
Expected Output: 2
Explanation: Jump 0->1 (value 3), then 1->4. Two jumps is minimal.
Input: ([2, 3, 0, 1, 4],)
Expected Output: 2
Explanation: Jump 0->1, then 1->4, skipping the zero at index 2. Two jumps.
Input: ([0],)
Expected Output: 0
Explanation: n == 1: already at the last index, so zero jumps are needed.
Input: ([],)
Expected Output: 0
Explanation: Empty array edge case: defined as needing 0 jumps.
Input: ([3, 2, 1, 0, 4],)
Expected Output: -1
Explanation: The zero at index 3 blocks progress; index 4 is unreachable, so return -1.
Input: ([1, 1, 1, 1, 1],)
Expected Output: 4
Explanation: Every value is 1, forcing one jump per step: 0->1->2->3->4, four jumps.
Input: ([5, 0, 0, 0, 0, 0],)
Expected Output: 1
Explanation: A single large jump from index 0 reaches the last index directly.
Input: ([1, 2, 3],)
Expected Output: 2
Explanation: 0->1 (value 2 lets you overshoot but the end is index 2), then 1->2. Two jumps.
Input: ([2, 0, 0],)
Expected Output: 1
Explanation: Index 0 (value 2) jumps straight to the last index in one move.
Input: ([1, 0, 1],)
Expected Output: -1
Explanation: Index 1 has value 0, a dead end before index 2; unreachable, return -1.
Hints
- Think of it as BFS in layers: every index reachable in k jumps forms one layer.
- Track cur_end (farthest reachable with the jumps taken so far) and farthest (farthest reachable with one more jump from the current layer).
- Only increment the jump count when i reaches cur_end — that is the moment you are forced to spend another jump to advance the frontier.
- Guard against unreachable inputs: if i ever exceeds farthest you are stranded, return -1. Also handle n == 1 (0 jumps) up front.