Gas Station
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
There are n gas stations arranged in a circle. You are given two integer arrays of length n:
- gas[i] is the amount of fuel available at station i.
- cost[i] is the amount of fuel needed to drive from station i to the next station, i + 1 (and from the last station back to station 0).
You start with an empty tank at one of the stations and want to drive clockwise all the way around the circle exactly once, returning to your starting station. At each station you first collect that station's gas[i], then spend cost[i] to reach the next station. Your tank may hold an unlimited amount of fuel, but it can never drop below zero at any point along the way.
Return the index of the starting station from which you can complete the full loop. If completing the loop is impossible from any station, return -1.
It is guaranteed that if a valid starting station exists, it is **unique**.
Examples
Constraints
- `n == gas.length == cost.length`
- `1 <= n <= 10^5`
- `0 <= gas[i], cost[i] <= 10^4`
- If a solution exists, it is guaranteed to be unique.
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.