The "Cheapest Flights Within K Stops interview question" is a shortest-path problem with an additional constraint. You are given a set of cities connected by flights with specific costs. Your goal is to find the minimum price to fly from a src city to a dst city, but you are limited to a maximum of k stops (meaning at most total flights).
This "Cheapest Flights Within K Stops coding problem" is a favorite of companies like Apple, Uber, and Airbnb. It tests whether a candidate can adapt standard shortest-path algorithms (like Dijkstra's) to include a "budget" of stops. It evaluates the ability to handle multi-dimensional state (cost and stops) and prevents simple greedy solutions from working correctly.
This problem can be solved using Breadth-First Search (BFS), Dijkstra's Algorithm, or Bellman-Ford.
min_costs to prune paths that are more expensive than what we've already found at a given city.Flights: (A->B, 100), (B->C, 100), (A->C, 500), .
A -> B -> C. Cost: 200. Stops: 1. (Valid).A -> C. Cost: 500. Stops: 0. (Valid).
Result: 200.
If , only Path 2 is valid. Result: 500.min_costs array to prune search branches, leading to exponential growth in explored paths.Master "Shortest Path" algorithms. This problem is the perfect example of why you must understand the why behind algorithms like Dijkstra. Practice identifying when a problem requires tracking an extra state variable beyond just the weight/cost.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Network Delay Time | Medium | Solve | |
| Find Edges in Shortest Paths | Hard | Solve | |
| The Maze II | Medium | Solve | |
| Minimum Edge Reversals So Every Node Is Reachable | Hard | Solve | |
| Minimize the Maximum Edge Weight of Graph | Medium | Solve |