Reported April 2022
Airbnbgraph

Cheapest Flight with Its Path

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

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

Airbnb reported this one in April 2022, and the trap is in the constraints. With n up to 100 and 5000 flights, trying every path blows up fast, so enumerating routes is out. It's Cheapest Flights Within K Stops with a twist: you return the actual path, and ties break on fewer flights, then lexicographically smaller sequence. That tie-break is where people lose points. If you blank mid-assessment, StealthCoder runs invisibly as a safety net and reads the problem on screen. Know the shape first: a layered shortest path where the number of flights is part of the state.

The problem

There are n cities numbered from 0 to n - 1. Each flight [from, to, price] is a directed edge with a positive price.
Find a minimum-cost route from src to dst that uses at most k intermediate stops, which is at most k + 1 flights.
Return an integer array whose first element is the total price and whose remaining elements are the visited city sequence from src through dst. If no valid route exists, return [-1].
When several routes have the same minimum price, prefer the route with fewer flights. If a tie remains, prefer the lexicographically smaller city sequence.

Function
cheapestFlightPath(n: int, flights: int[][], src: int, dst: int, k: int) → int[]

Examples
Example 1
n = 4
flights = [[0,1,100],[1,2,100],[2,3,100],[0,2,500],[1,3,600]]
src = 0
dst = 3
k = 2
return = [300,0,1,2,3]
The route 0 → 1 → 2 → 3 uses three flights, has two intermediate stops, and costs 300.
Example 2
n = 3
flights = [[0,1,100],[1,2,100],[0,2,500]]
src = 0
dst = 2
k = 0
return = [500,0,2]
No intermediate stop is allowed, so only the direct flight is eligible.

Constraints
1 <= n <= 100.
0 <= flights.length <= 5000.
flights[i].length == 3.
0 <= from, to, src, dst < n.
from != to, and no two flights have the same ordered endpoint pair.
1 <= price <= 10^6.
0 <= k < n.
Every valid route price fits in a signed 32-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The core is Bellman-Ford limited to k+1 rounds, or a layered DP where dist[j][v] is the best cost to reach v using exactly j flights. Plain Dijkstra on city alone fails because a cheaper route with too many stops can hide a valid one. Keep state as (city, flights used). For the tie-breaks, compare candidates by (cost, flights, path sequence), and store a parent pointer or the full path per state. With n at most 100 and k under n, storing paths is cheap. The common pitfall is updating in place during a round, which lets one round use two flights. Copy the previous layer first. Scan layers 1 to k+1 for dst and pick the best by your tuple. If it's unreachable, return [-1]. StealthCoder is the hedge if the tie-break logic tangles live.

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 Cheapest Flight with Its Path 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 Airbnb's OA.

Airbnb 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.

Cheapest Flight with Its Path FAQ

How hard is this Airbnb question really?+

Medium on the base problem, harder because of the path output and tie-breaks. If you know Cheapest Flights Within K Stops, the extra work is tracking paths and comparing by cost, flight count, then lexicographic order. Expect most bugs there, not in the shortest path part.

What's the trick to avoid brute force?+

Make flights used part of the state. Run k+1 rounds of Bellman-Ford style relaxation, or fill a table of best cost per city per flight count. That's roughly (k+1) times E work, easily fine for 5000 flights and 100 cities.

Why does plain Dijkstra fail here?+

Dijkstra keeps one best cost per city, but a cheaper arrival with more stops can be unusable later. A pricier arrival with fewer stops might be the only one that reaches dst within k. You need (city, stops) as the state, or the layered approach.

How do I handle the tie-breaking rules?+

Compare candidates as a tuple: total price, number of flights, then the city sequence. Since n is at most 100, store the full path at each state and compare lists directly. Layers by flight count already give you the fewer-flights ordering when you scan them.

How do I prepare in 48 hours?+

Solve Cheapest Flights Within K Stops until the layered relaxation is automatic. Then add path reconstruction and the tie-break comparison. Test the edge cases: k = 0, no route, and two equal-cost routes with different lengths. Don't spend time on unrelated graph topics.

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

OA at Airbnb?
Invisible during screen share
Get it