Pick the Starting Depot for an Electric Shuttle Loop

Last updated: November 29, 2026

Quick Overview

An airport shuttle runs a fixed circular loop of depots. At depot i it can charge charge[i] units, and driving to the next depot costs cost[i] units. Starting empty, return the index of the depot where the shuttle can start and complete the whole loop, or -1 if no start works (the answer is unique when it exists).

Uber
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


An airport shuttle runs a fixed circular loop of depots. At depot i it can charge charge[i] units, and driving to the next depot costs cost[i] units. Starting empty, return the index of the depot where the shuttle can start and complete the whole loop, or -1 if no start works (the answer is unique when it exists).

A greedy problem in the style of Uber's technical screens, set in Uber's airport and corporate shuttle products. The interviewer wants the one-pass argument: if you run dry between start and j, no depot in that range can be the start either.

What the Interviewer Expects
  • Checks total charge against total cost first
  • Scans once with a running tank
  • Resets the candidate start after a failure
  • Proves why skipped depots cannot be valid starts
  • States O(n) time and O(1) space
Key Topics to Cover
Greedy
Prefix sums
Circular arrays
Proof by contradiction
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if the battery has a maximum capacity?
  • How would you list every valid start if uniqueness is not guaranteed?
  • What if the shuttle can run the loop in either direction?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: two equal-length lists of non-negative ints. Output: a start index or -1. The battery has no cap and starts empty. Brute force tries every start in O(n2)O(n^2)....

High-Level Design

If the sum of charges is less than the sum of costs, no start works. Otherwise scan once with tank += charge[i] - cost[i]. When the tank goes negative at depot i, the current candidate fails, and so...


Submit Your Answer
Markdown supported