Cheapest Flights Within K Stops
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given n cities numbered from 0 to n - 1, connected by one-way flights. The flights are described by a list flights, where each entry flights[i] = [from, to, price] means there is a directed flight from city from to city to costing price.
You are also given three integers: a starting city src, a destination city dst, and an integer k.
Return the **cheapest total price** to travel from src to dst using **at most k stops** (intermediate cities). If no such route exists, return -1.
A "stop" is an intermediate city between src and dst. For example, a direct flight from src to dst uses 0 stops, while a route src -> a -> dst uses 1 stop. This means a valid route may use at most k + 1 flights in total.
Examples
Constraints
- 1 <= n <= 100
- 0 <= flights.length <= n * (n - 1)
- flights[i].length == 3
- 0 <= from, to < n
- from != to
- 1 <= price <= 10^4
- There will not be any duplicate directed flights (same from and to pair).
- 0 <= src, dst < n
- src != dst
- 0 <= k < n
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.