← 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

Hiring now

PhonePe has 114 open roles right now

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
  • Software Engineer, Backend (3-5 years)

    Pune, Maharashtra, India

  • Software Engineer , Backend (5-7 years)

    Pune, Maharashtra, India

  • Software Engineer, App-Android

    Bengaluru, Karnataka, India

Or practice this question first

Next question