Intermediatecoding Verified
Gas stations are arranged in a circle with gas[i] available and cost[i] to reach the next one. Find the starting station from which you can complete the circuit, or -1.
ReportedSource: GeeksforGeeks interview experience: Swiggy SDE-1 off-campus 2021
Quick answerFree
If total gas < total cost return -1; otherwise sweep with a running tank and reset start to i+1 whenever the tank goes negative. O(n), O(1).
Detailed answer· 98 words
Interview PrepUnlock the full answer with Interview Prep
Get a 98-word model answer and a runnable reference solution, plus 28 more Algorithms questions.
Also in Pro (₹499 / 90 days).
Reference solution· runs in the browser sandbox
Interview PrepUnlock with Interview Prep
+ 28 more Algorithms questions in Interview Prep, each with the full answer.