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 Bloomberg LP Onsite Interview questions
- Pascal's TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Valid Triangle NumberPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 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
- Largest Number Below N from Reusable DigitsByteDance · PHONE SCREENByteDance interview questions
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Jump GamePaytm · PHONE SCREENPaytm 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
- Remove the Fewest Magic BeansThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Capacity to Ship Packages Within DaysMicrosoft · OA · ONSITE INTERVIEWMicrosoft interview questions