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
November 29, 20260
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample 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 ....
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...