Back to Practice
#0283

Gas Station

MediumDSA28 min20 XP

Problem

Given gas and cost arrays, return the starting index that lets you travel around the circle once, or -1 if impossible.

Why This Matters

Gas Station is a famous greedy reset problem: once a start fails, every station inside that failed segment fails too.

Function Signature

def can_complete_circuit(gas, cost):

Examples

Example 1
Inputgas = [1,2,3,4,5], cost = [3,4,5,1,2]
Output3

Starting at index 3 gives enough surplus to finish the loop.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Start at three
Inputgas [1,2,3,4,5], cost [3,4,5,1,2]
Expected3

Station 3 is the first viable start after failed prefixes.

Impossible total
Inputgas [2,3,4], cost [3,4,3]
Expected-1

Total gas is less than total cost.

Hidden Test Categories
start at zeroall exact matchessingle station