Cheapest Flight with Its Path
Updated
Problem statement
There are n cities, numbered from 0 to n - 1. Each flight [from, to, price] goes one way only, from city from to city to. Every flight has a positive price.
Find the cheapest route from city src to city dst. A stop is a city where the route lands between src and dst. The route may make at most k stops, so it uses at most k + 1 flights.
Return an integer array. Its first element is the total price. The rest of the array lists the cities on the route in order, starting with src and ending with dst. If no route works, return [-1].
Several routes may share the lowest price. Then pick the one with fewer flights. If there is still a tie, compare the city lists from the left. At the first place where they differ, pick the route whose city number is smaller.
Function
cheapestFlightPath(n: int, flights: int[][], src: int, dst: int, k: int) → int[]Examples
Example 1
n = 4flights = [[0,1,100],[1,2,100],[2,3,100],[0,2,500],[1,3,600]]src = 0dst = 3k = 2return = [300,0,1,2,3]With at most two stops, the route may use up to three flights. The route 0 → 1 → 2 → 3 uses three flights, makes two stops, and costs 300. Every other route to the destination costs more. So the answer is that price, followed by the four cities of the route in order.
Example 2
n = 3flights = [[0,1,100],[1,2,100],[0,2,500]]src = 0dst = 2k = 0return = [500,0,2]No stop is allowed, so only a direct flight from the start city to the destination counts. The route through the middle city is cheaper, but it needs one stop. So the answer is the direct flight's price, followed by its start and end cities.
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 both the samefromand the sameto.1 <= price <= 10^6.0 <= k < n.- Every valid route price fits in a signed 32-bit integer.
More Airbnb Online Assessment questions
- Count Card Duel RoundsOA · Seen Nov 2026
- Count Lamps Lighting Each PointOA · Seen Nov 2026
- Simulate Orchard Rot SpreadOA · Seen Nov 2026
- Sort Adjacent PairsOA · Seen Nov 2026
- In-Memory File SystemOA · Seen Nov 2026
- Task Management SystemOA · Seen Nov 2026
- Recipe Management SystemOA · Seen Nov 2026
- Progressive Banking SystemOA · Seen Nov 2026
- Credit Accounts with Scheduled TransfersOA · Seen Oct 2026
- Parcel Attribute Store with TTLOA · Seen Oct 2026
Similar questions at other companies
- Drone Delivery with Battery Swap StationsCisco · OACisco interview questions
- Minimum Path Cost with Edge DiscountsSkyscanner · ONSITE INTERVIEWSkyscanner interview questions
- Maximum Gain on a Budgeted Returning WalkNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Cities on Shortest Delivery RoutesMathWorks · OAMathWorks interview questions
- Shortest Paths in a Nonnegative Directed GraphNation with NaMo · PHONE SCREEN
- Cheapest Flights Within K StopsGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- All-Pairs Shortest PathsMorgan Stanley · OA · ONSITE INTERVIEWMorgan Stanley interview questions
- Cheapest Flights Within K StopsInMobi · OAInMobi interview questions
- Minimum Leaf Path Sum over All RootsInfosys · OAInfosys interview questions
- Largest Consistent Visiting GroupAuxoAI · OA