← Questions
Fewest jumps between two indices of a circular array
0
I was asked this
No reports yet. Sign in to say where you were asked it.
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—0ifstart == 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 thann? - 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]) % nin Python, an extra+ nin other languages) and for distances larger thann. - A value of 0 or a distance that is a multiple of
nis 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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
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
