Reported August 2026
Microsoftshortest path

Minimum Fuel Cost Between Cities

Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Microsoft OA. Under 2s to a working solution.
Founder's read

Microsoft reportedly put this one in front of candidates in August 2026, and it looks like a fuel-tank DP problem until you see what it really is. Unlimited tank, buy anywhere, so it collapses into a shortest path where each edge costs distance times the cheapest price seen so far on your route. That's Dijkstra with a twist in the state. If you blank on the state definition during the OA, StealthCoder is the safety net running invisibly while you work. Know the trick before you open the timer.

The problem

There are n cities connected by undirected roads. Each road [u, v, distance] consumes distance units of fuel. Fuel costs fuelPrices[i] per unit in city i.
You start in start with an empty, unlimited-capacity fuel tank. At any visited city, you may buy any nonnegative amount of fuel before taking another road. Return the minimum total cost required to reach destination, or -1 if it is unreachable.

Function
minFuelCost(n: int, roads: int[][], fuelPrices: int[], start: int, destination: int) → long

Examples
Example 1
n = 3
roads = [[0,1,3],[1,2,6]]
fuelPrices = [5,2,9]
start = 0
destination = 2
return = 27
Buy 3 units at city 0 for 15, then 6 units at city 1 for 12. The total is 27.
Example 2
n = 4
roads = [[0,1,5],[0,2,1],[2,1,1],[1,3,2],[2,3,10]]
fuelPrices = [10,8,2,9]
start = 0
destination = 3
return = 16
Travel 0 -> 2 -> 1 -> 3. The first unit costs 10; after reaching city 2, the remaining three units cost 2 each.

Constraints
1 <= n <= 200
0 <= roads.length <= 2 * 10^4
Every road has two distinct valid endpoints and a positive distance.
1 <= fuelPrices[i] <= 10^6
0 <= start, destination < n

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: with an unlimited tank, you never need to carry fuel you bought at a price higher than one you'll see later. So the state is (city, cheapest price so far on this path). Run Dijkstra over that state. Moving along an edge costs distance times the current best price, and arriving at city v updates the best to min(best, price[v]). Buy at the start city for the first leg. The pitfall is running plain Dijkstra on city alone. That fails because the same city reached with a cheaper best price can beat a lower-cost arrival. With n up to 200 and prices up to 10^6, the state space is small, so a heap works fine. Use 64-bit for the totals. Return -1 if the destination never pops. If you freeze on the state design in the live OA, StealthCoder is the hedge that gets you the working structure.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Minimum Fuel Cost Between Cities cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Microsoft's OA.

Microsoft reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Fuel Cost Between Cities FAQ

What's the real trick in Minimum Fuel Cost Between Cities?+

Track the cheapest fuel price seen along the path as part of the state. Each edge then costs distance times that price. Run Dijkstra on (city, best price). Without the price in the state, you'll get wrong answers on graphs where a pricier route passes through a cheap city.

Is this just a standard shortest path problem?+

Not quite. Plain Dijkstra on city alone breaks because cost depends on history, namely the cheapest price you've passed. Add that value to the state and it becomes a normal Dijkstra over an expanded graph. The 200-city limit keeps the state count manageable.

Do I need to track how much fuel is in the tank?+

No. The tank is unlimited, so you can model it as buying fuel for each edge at the cheapest price seen so far. Buying early at a cheap city is equivalent to carrying it. That removes the fuel level from the state and keeps things small.

What edge cases did Microsoft candidates likely hit in August 2026?+

Start equals destination should return 0. Unreachable destination returns -1. Distances times prices can exceed 32-bit range, so use 64-bit integers. Parallel roads between the same cities are possible given 2 * 10^4 roads and only 200 nodes, so don't assume a simple graph.

How do I prepare for this in 48 hours?+

Write Dijkstra with a heap from memory until it's automatic. Then practice one variant where the state carries extra info, like remaining stops or best price. Solve this problem once end to end, and check both examples by hand. Expected answers are 27 and 16.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Microsoft.

OA at Microsoft?
Invisible during screen share
Get it