← All questions
MediumCoding

Gas Station

Asked atAmazonCCleartripGoldman SachsPhonePeFlipkart
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Compare total available gas with total travel cost to determine global feasibility.
  2. 02Scan once while maintaining the tank balance from the current candidate start.
  3. 03If the balance becomes negative at i, discard every start from the current candidate through i.
  4. 04Reset the candidate to i + 1 and return it after the scan if the total balance is non-negative.

Reference answer

Then expect these follow-ups

  • Why can every station between the failed start and i be discarded?

    Tests: greedy proof

  • How would the answer change if the tank had a fixed maximum capacity?

    Tests: model adaptation

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question