Skip to main content
All Questions

Find the path from source to destination with fewest stations, then fewest roads, under refueling rules.

Unlock detailed company stats for this questionUpgrade

You are given N cities and M roads, each road connecting two different cities directly. Going by car from city x to city y via any road costs 1 kg of fuel. The car starts at the source city and must reach the destination city, initially with P kg of fuel. Some cities have refueling stations: whenever you visit a city with a station, you must refuel to exactly P kg again. You may visit the same road multiple times, and each visit counts separately in the final answer. The source city is never a station.

Priorities, in order:

  1. Never reach negative fuel at any point.
  2. Visit the minimum number of stations.
  3. Use the minimum number of direct roads.

Output {min_stations, min_roads} for the path, and report if it is not possible (you might run out of fuel on the way).

Format · Onsite DSA round.

Community answers

No answers contributed by the community yet.

Related courses

Course

Forward Deployed Engineering Interviews

Land your next role in forward deployed engineering (FDE) at top companies like Google, Anthropic, OpenAI, and startups. Learn from mock interviews, frameworks, and advice from senior candidates. Perfect your system design, coding, and behavioral interviews.

Course

Behavioral Interviews: A Practical Guide for Engineers

Get offers at the level you deserve. Prep for senior+ behavioral interviews at FAANG and Tier-1 companies. Made with staff and principal engineers from Meta, Netflix, Google, and more. Avoid downleveling, elevate past projects, and stop sounding like a robot.