Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA

Problem: The gas-station problem: gas = [1, 2, 3, 4, 5] and cost = [3, 4, 5, 1, 2] around a circular route of 5 stations, starting with an empty tank. What is the index (0-based) of the starting station from which you can complete the circuit?

  1. A. 4
  2. B. 2
  3. C. 5
  4. D. 3

Correct answer: D. 3

If total gas is at least total cost, a valid start exists; when the running tank drops below zero, the next station is the only candidate worth checking. Starting at index 0 fails here.

More Coding Interview Problems questions

Exams that ask this topic