← Questions

Fewest jumps between two indices of a circular array

0
UberCodingLast asked Jan 2026

A coding interview question asked in Uber's technical phone screen, last asked Jan 2026. It tests bfs and arrays.

You get a circular array arr of length n. From index i you must jump exactly arr[i] positions, either left or right, wrapping around the ends. That takes you to (i + arr[i]) mod n or (i - arr[i]) mod n. Given a start index and a target index, return the fewest jumps from start to target, or -1 if the target cannot be reached.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • min_jumps(arr: list[int], start: int, target: int) -> int — 0 if start == target.

Example

min_jumps([2, 1, 2, 3], 0, 3)     ->  -1    # 0 only reaches 2, and 2 only reaches 0
min_jumps([1, 1, 1, 1], 0, 2)     ->  2     # 0 -> 1 -> 2, or 0 -> 3 -> 2
min_jumps([3, 1, 2, 5, 1], 0, 4)  ->  2     # 0 -> 2 (left by 3), 2 -> 4 (right by 2)

Constraints

  • 1 <= n <= 2 × 10^5, 0 <= arr[i] <= 10^9, 0 <= start, target < n.

Clarifying questions

  • Is the jump distance the value at the current index? (Yes.)
  • Can arr[i] be 0, or larger than n?
  • Must I return the path, or only the count?

What a strong answer covers

  • Modeling indices as nodes with at most two outgoing edges, and breadth-first search from start, since every jump costs the same.
  • Correct modular arithmetic for negative values ((i - arr[i]) % n in Python, an extra + n in other languages) and for distances larger than n.
  • A value of 0 or a distance that is a multiple of n is a self-loop, which the visited set handles.
  • O(n) time and space.
  • Tests: start equals target, unreachable target, n = 1, very large values.

Follow-ups

  • Return the path.
  • You may also jump to any index holding the same value as the current one. How do you keep BFS linear?
  • Answer many (start, target) queries on the same array.

Where it's asked

  • Uber · Technical phone screen · Last asked Jan 202660 min · One medium problem with a follow-up, or two short ones; sometimes object-oriented design

What it tests

bfs · arrays

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding