Pick the Starting Depot for a Circular Delivery Loop on Limited Fuel

Last updated: November 29, 2026

Quick Overview

A delivery van drives a circular route of n depots, refueling fuel[i] at depot i and spending cost[i] to reach depot i + 1. Starting with an empty tank, return the index of the depot where it can start and complete the full loop, or -1 if none exists (the answer is unique if it exists).

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

0

0

0 solved


A delivery van drives a circular route of n depots, refueling fuel[i] at depot i and spending cost[i] to reach depot i + 1. Starting with an empty tank, return the index of the depot where it can start and complete the full loop, or -1 if none exists (the answer is unique if it exists).

An Amazon technical screen greedy question rooted in route planning. The interviewer expects the one-pass solution and, more importantly, the two-part proof: total surplus decides feasibility, and a failed prefix rules out every start inside it.

What the Interviewer Expects
  • Checks total fuel minus total cost for feasibility
  • Resets the start after any negative running tank
  • Proves why starts inside a failed segment also fail
  • Runs in one O(n) pass
  • Handles n = 1
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
  • Return all valid starts when the answer is not unique.
  • The van has a tank capacity limit: does greedy still work?
  • Find the minimum starting fuel needed from depot 0.
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: lists fuel and cost of equal length n. Output: start index or -1. The van fills fuel[i] on arrival at i, then must have at least cost[i] to drive on....

High-Level Design

If sum(fuel) < sum(cost), no start works. Otherwise scan once with a running tank from a candidate start. When the tank drops below zero at i, no start between the candidate and i can work either: eac...


Submit Your Answer
Markdown supported