Magicsheet logo

Jump Game III

Medium
64.2%
Updated 6/1/2025

Jump Game III

1. What is this problem about?

The Jump Game III interview question is a reachability problem. You are given an array of non-negative integers and a starting index. From any index i, you can jump to i + arr[i] or i - arr[i], provided the new index is within the array bounds. Your goal is to determine if you can ever reach an index where the value is 0.

2. Why is this asked in interviews?

Companies like Goldman Sachs and Pinterest ask the Jump Game III coding problem to test a candidate's familiarity with Graph Traversal. Even though the input is an array, the jumps create a directed graph. It evaluation your ability to use BFS or DFS and manage a "visited" state to avoid infinite loops.

3. Algorithmic pattern used

This problem follows the Graph Traversal (BFS or DFS) pattern.

  1. Starting Point: Push the start index into a stack (DFS) or queue (BFS).
  2. Visited Set: Maintain a boolean array or set to track which indices you've already checked.
  3. Exploration: While the queue/stack is not empty:
    • Pop an index curr.
    • If arr[curr] == 0, return true.
    • Calculate two potential next indices: curr + arr[curr] and curr - arr[curr].
    • For each, if it’s within bounds and not visited, add it to the traversal structure and mark it visited.
  4. Failure: If the traversal completes without finding a 0, return false.

4. Example explanation

arr = [4, 2, 3, 0, 3, 1, 2], start = 5

  1. Start at index 5 (value 1).
  2. Jumps: 5+1=65+1=6 and 51=45-1=4.
  3. At index 4 (value 3): 43=14-3=1 and 4+3=74+3=7 (out).
  4. At index 6 (value 2): 62=46-2=4 (visited) and 6+2=86+2=8 (out).
  5. At index 1 (value 2): 1+2=31+2=3 and 12=11-2=-1 (out).
  6. At index 3 (value 0): Goal reached! Result: True.

5. Common mistakes candidates make

  • No Visited Check: Failing to track visited indices, which leads to an infinite loop if the jumps form a cycle.
  • Boundary Errors: Not checking if nextIndex < 0 or nextIndex >= arr.length.
  • Recursive Depth: Using DFS on a very large array without considering stack limits.

6. Interview preparation tip

Whenever you see "from position X you can go to Y or Z," think BFS/DFS. If the edge weights are all 1 (or irrelevant), BFS is usually preferred for finding reachability or shortest paths in a Graph interview pattern.

Similar Questions