← Questions

Smallest starting index that never leaves on the left

0
UberCodingLast asked Dec 2025

A coding interview question asked in Uber's coding round, last asked Dec 2025. It tests arrays and simulation.

You get an integer array jump of length n. From index i your next position is i + jump[i]: a positive value moves right, a negative one moves left and 0 stays put. Starting from some index you keep applying this rule. If you land below index 0 you have left on the left; if you land at n or beyond you have left on the right and stop. You may also loop forever inside the array. Return the smallest starting index from which you never leave on the left, or -1 if there is none.

Interface

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

  • smallest_safe_start(jump: list[int]) -> int — leaving on the right and looping forever are both safe.

Example

smallest_safe_start([-1, -1, -1, -1, 2])  ->  4    # 0..3 all walk down to -1; 4 jumps to 6 and leaves on the right
smallest_safe_start([0, -2, 1])           ->  0    # 0 stays on itself forever
smallest_safe_start([2, -1, -5, 1])       ->  3    # 0 -> 2 -> -3; 1 -> 0 -> 2 -> -3; 2 -> -3; 3 -> 4 leaves right
smallest_safe_start([-1, -2])             ->  -1

Constraints

  • 0 <= n <= 2 × 10^5, -10^9 <= jump[i] <= 10^9.
  • Aim for O(n) time.

Clarifying questions

  • Is looping forever inside the array safe? (Yes.)
  • Is landing exactly at n leaving on the right?
  • Can the values be huge, so that one jump leaves immediately?
  • What should an empty array return?

What a strong answer covers

  • Each index has exactly one next index, so the array is a functional graph: every walk ends on the left, ends on the right, or enters a cycle.
  • The simple solution, walking from each start, is O(n²) in the worst case.
  • O(n): label every index once. Walk from an unlabeled index, keeping the path; when the walk exits or reaches a labeled index or an index already on the current path (a cycle, safe), assign that outcome to every index on the path.
  • Or reverse the edges and search from the two exits: every index that reaches the left exit is unsafe, everything else is safe.
  • Then return the smallest safe index.
  • Tests: all safe, none safe, a cycle, a self-loop, huge jumps.

Follow-ups

  • Return every safe starting index.
  • For each start, return how many jumps it takes to leave, or -1 if it loops.
  • The array changes: one value is updated at a time. How would you keep the answer current?

Where it's asked

  • Uber · Coding · Last asked Dec 202560 min · Two rounds; one is often object-oriented design (a parking lot, a meeting scheduler, an expression evaluator) · No AI, except a newer AI-assisted debugging round in some senior loops

What it tests

arrays · simulation

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