Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Gas Station / Oasis / Car Path
00:00
5 left

Gas Station / Oasis / Car Path

EasyPython

Problem

A Bloomberg Terminal field vehicle must travel through a circular sequence of service stations. At station i, it can collect fuel[i] units of fuel, then needs distance[i] units to reach station (i + 1) % n.

Implement can_complete_circuit(fuel, distance) to return an index from which the vehicle can visit every station exactly once and return to its starting station. Return -1 if no such starting station exists. Assume the vehicle starts with an empty tank and has unlimited tank capacity.

Formal Specification

  • fuel: a list of n nonnegative integers, where fuel[i] is fuel available at station i.
  • distance: a list of n nonnegative integers, where distance[i] is fuel required to travel from station i to the next station.
  • Return an integer index in [0, n - 1], or -1 when completing the circuit is impossible.
  • The input lists have equal length and contain at least one station.

Constraints

  • 1 <= n <= 10^5
  • 0 <= fuel[i], distance[i] <= 10^4
  • fuel and distance have equal length
  • Return any valid starting index if one exists

Function Signature

def can_complete_circuit(fuel, distance):
Interviewer

Your question is Gas Station / Oasis / Car Path. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.