134. Gas Station
Asked at Google, Meta, Amazon, Microsoft and 5 more
Difficulty
Medium
Time
O(n)
Space
O(1)
Pattern
Array
Companies
9
Problem
There are n gas stations on a circular road. At station i you can fill up gas[i] units of gas. Driving from station i to the next station costs cost[i] units of gas.
You start at one station with an empty tank. Your tank has no size limit, so you can hold as much gas as you like. You must drive around the circle once, always moving forward (station i → station i + 1, and the last station wraps back to station 0).
Return the index of the station where you should start so that you can finish the whole trip without ever running out of gas. If no start works, return -1. If a valid start exists, the problem guarantees it is unique.
A circular route with gas and cost
Constraints
- ●
n == gas.length == cost.length - ●
1 <= n <= 10^5 - ●
0 <= gas[i], cost[i] <= 10^4 - ●
The answer is unique when it exists
Examples
gas = [1,2,3,4,5], cost = [3,4,5,1,2]3gas = [2,3,4], cost = [3,4,3]-1Asked At
9 companies| Company | Difficulty | |
|---|---|---|
| Medium | View all Google questions → | |
| Meta | Medium | View all Meta questions → |
| Amazon | Medium | View all Amazon questions → |
| Microsoft | Medium | View all Microsoft questions → |
| Apple | Medium | View all Apple questions → |
| Uber | Medium | View all Uber questions → |
| Bloomberg | Medium | View all Bloomberg questions → |
| Goldman Sachs | Medium | View all Goldman Sachs questions → |
| TikTok | Medium | View all TikTok questions → |
How to Think About It
First check: if total gas < total cost, it's impossible. Return -1. This is a necessary condition.
If total gas >= total cost, a solution is guaranteed to exist. The question is where to start.
Greedy insight: if you run out of gas between start and station j, no station between start and j can be the answer. They all have less gas available than start.
Algorithm: iterate with a running tank. If tank goes negative, reset start to i+1 and tank to 0.
Edge cases: one station, all stations have enough gas, circular route where start is the last station.
Optimal Approach
Step 1: If sum(gas) < sum(cost), return -1.
Step 2: Initialize tank = 0, start = 0.
Step 3: For i from 0 to n-1:
tank += gas[i] - cost[i]
If tank < 0:
start = i + 1
tank = 0
Step 4: Return start.
Time: O(n). Space: O(1).
Greedy reset of the start
What Trips People Up in Real Interviews
Not checking the total gas vs total cost first. If total gas < total cost, no solution exists — return -1 immediately.
Trying all starting positions. That's O(n²). The greedy approach is O(n).
Forgetting that the route is circular. After the last station, you return to the first.
Not resetting the tank and start when tank goes negative. If you run out of gas, no station between start and the current position can be the answer.
Resetting the tank to gas[i] - cost[i] instead of 0 when tank goes negative. The tank should reset to 0 and start moves to i + 1. Setting it to a non-zero value might keep a negative tank, breaking the greedy invariant.
Solution Code
O(n) timedef canCompleteCircuit(gas, cost):
if sum(gas) < sum(cost):
return -1
tank = 0
start = 0
for i in range(len(gas)):
tank += gas[i] - cost[i]
if tank < 0:
start = i + 1
tank = 0
return startFrequently Asked Questions
What is the Gas Station problem?
There are n gas stations along a circular route. You have two arrays: `gas[i]` is the amount of gas at station i, and `cost[i]` is the cost to travel to the next station. Return the starting gas station index if you can travel around the circuit once, otherwise return -1.
How do you solve Gas Station?
The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Gas Station?
Gas Station is asked at Google, Meta, Amazon, Microsoft, Apple, Uber, Bloomberg, Goldman Sachs, TikTok. It is a medium difficulty problem.
What are common mistakes on Gas Station?
- Not checking the total gas vs total cost first. If total gas < total cost, no solution exists — return -1 immediately.
- Trying all starting positions. That's `O(n²)`. The greedy approach is `O(n)`.
- Forgetting that the route is circular. After the last station, you return to the first.
- Not resetting the tank and start when tank goes negative. If you run out of gas, no station between start and the current position can be the answer.
- Resetting the tank to `gas[i]` - `cost[i]` instead of 0 when tank goes negative. The tank should reset to 0 and start moves to i + 1. Setting it to a non-zero value might keep a negative tank, breaking the greedy invariant.