Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an array of non-negative integers arr, you are initially positioned at start index of the array. When you are at index i, you can jump to i + arr[i] or i - arr[i], check if you can reach any index with value 0.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [4,2,3,0,3,1,2], start = 5 Output: true Explanation: All possible ways to reach at index 3 with value 0 are: index 5 -> index 4 -> index 1 -> index 3 index 5 -> index 6 -> index 4 -> index 1 -> index 3
Example 2:
Input: arr = [4,2,3,0,3,1,2], start = 0 Output: true Explanation: One possible way to reach at index 3 with value 0 is: index 0 -> index 4 -> index 1 -> index 3
Example 3:
Input: arr = [3,0,2,1,2], start = 2 Output: false Explanation: There is no way to reach at index 1 with value 0.
Constraints:
1 <= arr.length <= 5 * 1040 <= arr[i] < arr.length0 <= start < arr.lengthProblem summary: Given an array of non-negative integers arr, you are initially positioned at start index of the array. When you are at index i, you can jump to i + arr[i] or i - arr[i], check if you can reach any index with value 0. Notice that you can not jump outside of the array at any time.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[4,2,3,0,3,1,2] 5
[4,2,3,0,3,1,2] 0
[3,0,2,1,2] 2
jump-game-ii)jump-game)jump-game-vii)jump-game-viii)maximum-number-of-jumps-to-reach-the-last-index)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1306: Jump Game III
class Solution {
public boolean canReach(int[] arr, int start) {
Deque<Integer> q = new ArrayDeque<>();
q.offer(start);
while (!q.isEmpty()) {
int i = q.poll();
if (arr[i] == 0) {
return true;
}
int x = arr[i];
arr[i] = -1;
for (int j : List.of(i + x, i - x)) {
if (j >= 0 && j < arr.length && arr[j] >= 0) {
q.offer(j);
}
}
}
return false;
}
}
// Accepted solution for LeetCode #1306: Jump Game III
func canReach(arr []int, start int) bool {
q := []int{start}
for len(q) > 0 {
i := q[0]
q = q[1:]
if arr[i] == 0 {
return true
}
x := arr[i]
arr[i] = -1
for _, j := range []int{i + x, i - x} {
if j >= 0 && j < len(arr) && arr[j] >= 0 {
q = append(q, j)
}
}
}
return false
}
# Accepted solution for LeetCode #1306: Jump Game III
class Solution:
def canReach(self, arr: List[int], start: int) -> bool:
q = deque([start])
while q:
i = q.popleft()
if arr[i] == 0:
return True
x = arr[i]
arr[i] = -1
for j in (i + x, i - x):
if 0 <= j < len(arr) and arr[j] >= 0:
q.append(j)
return False
// Accepted solution for LeetCode #1306: Jump Game III
struct Solution;
use std::collections::VecDeque;
impl Solution {
fn can_reach(arr: Vec<i32>, start: i32) -> bool {
let n = arr.len();
let mut visited = vec![false; n];
let mut queue = VecDeque::new();
queue.push_back(start);
visited[start as usize] = true;
while let Some(i) = queue.pop_front() {
if arr[i as usize] == 0 {
return true;
} else {
let l = i - arr[i as usize];
let r = i + arr[i as usize];
if l >= 0 && !visited[l as usize] {
visited[l as usize] = true;
queue.push_back(l);
}
if r < n as i32 && !visited[r as usize] {
visited[r as usize] = true;
queue.push_back(r);
}
}
}
false
}
}
#[test]
fn test() {
let arr = vec![4, 2, 3, 0, 3, 1, 2];
let start = 5;
let res = true;
assert_eq!(Solution::can_reach(arr, start), res);
let arr = vec![4, 2, 3, 0, 3, 1, 2];
let start = 0;
let res = true;
assert_eq!(Solution::can_reach(arr, start), res);
let arr = vec![3, 0, 2, 1, 2];
let start = 2;
let res = false;
assert_eq!(Solution::can_reach(arr, start), res);
}
// Accepted solution for LeetCode #1306: Jump Game III
function canReach(arr: number[], start: number): boolean {
const q = [start];
for (const i of q) {
if (arr[i] === 0) {
return true;
}
if (arr[i] === -1 || arr[i] === undefined) {
continue;
}
q.push(i + arr[i], i - arr[i]);
arr[i] = -1;
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.