Gas Station
Updated
Problem statement
There are n gas stations on a circular route. gas[i] is the fuel available at station i, and cost[i] is the fuel needed to travel from station i to station (i + 1) mod n.
You start with an empty tank. Return the index of the station from which you can travel clockwise around the circuit exactly once without the tank becoming negative. Return -1 if no such station exists. If a solution exists, it is unique.
Function
canCompleteCircuit(gas: int[], cost: int[]) → intExamples
Example 1
gas = [1,2,3,4,5]cost = [3,4,5,1,2]return = 3Starting at station 3, the tank never becomes negative and returns to station 3 after visiting every station.
Example 2
gas = [2,3,4]cost = [3,4,3]return = -1Total available fuel is less than the total travel cost, so no start can complete the circuit.
Example 3
gas = [5]cost = [4]return = 0The single station supplies enough fuel for its only leg.
Constraints
1 <= n == gas.length == cost.length <= 100000.0 <= gas[i], cost[i] <= 10000.- If a valid starting station exists, it is unique.
More eBay Onsite Interview questions
- Extract Hashtags from a TweetONSITE INTERVIEW · Seen Nov 2026
- Validate Ada Integer LiteralONSITE INTERVIEW · Seen Nov 2026
- Count Distinct Pairs With Sum KONSITE INTERVIEW · Seen Nov 2026
- Diameter of Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Count Staircases with Strictly Increasing StepsONSITE INTERVIEW · Seen Oct 2026
- Hierarchical Permission CheckONSITE INTERVIEW · Seen Oct 2026
- Matrix MultiplicationONSITE INTERVIEW · Seen Oct 2026
- N-Queens Solution CountONSITE INTERVIEW · Seen Oct 2026
- Onion-Peel Layers of a Binary TreeONSITE INTERVIEW · Seen Oct 2026
- Container With Most WaterONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Minimum Piano Hand Raises and Thumb PositionsGoogle · ONSITE INTERVIEWGoogle interview questions
- Fair Capped Money DistributionGusto · PHONE SCREEN · ONSITE INTERVIEWGusto interview questions
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Minimize Total Weight After HalvingAmazon · ONSITE INTERVIEW
- Capacity to Ship Packages Within DaysMicrosoft · OA · ONSITE INTERVIEWMicrosoft interview questions
- Wizard Scroll Allocation StrategyJane Street · ONSITE INTERVIEWJane Street interview questions
- Maximize Compatible Car RidersUber Freight · PHONE SCREEN · ONSITE INTERVIEWUber Freight interview questions
- Maximize Monsters DefeatedSalesforce · ONSITE INTERVIEWSalesforce interview questions