Cheapest Trip Itinerary Within a Maximum Number of Stops
Last updated: November 29, 2026
Quick Overview
Given cities, one-way flights with prices, a source, a destination, and a maximum number of intermediate stops, return the cheapest total price and the route, or -1 if no route fits the stop limit.
Airbnb
November 29, 20260
0
0 solved
Given cities, one-way flights with prices, a source, a destination, and a maximum number of intermediate stops, return the cheapest total price and the route, or -1 if no route fits the stop limit.
Airbnb's trip planning ideas, which combine stays across several cities, raise questions about cheap routing under constraints. The interviewer checks whether you notice that plain Dijkstra breaks when a constraint is added, and how you fix it with state expansion or Bellman-Ford rounds.
What the Interviewer Expects
- Explain why vanilla Dijkstra with a single distance per city gives wrong answers
- Expand the state to (city, edges used)
- Prune dominated states correctly
- Return the path as well as the cost
- Compare with a k+1 round Bellman-Ford solution
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return the top three cheapest itineraries?
- How would you add a constraint on total travel time as well?
- Which approach is better when the graph is dense and k is small?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
n cities labeled 0..n-1, flights (u, v, price) with non-negative prices, max_stops intermediate cities, so at most max_stops + 1 flights. Output (cost, path) or (-1, [])....
High-Level Design
Standard Dijkstra settles a city at its cheapest cost, but that cheap route may use too many flights, while a pricier route with fewer flights could still reach the destination. So the state is (city,...