Wasn't ready for a graph question in a frontend screen, so I froze for a bit.
Model the flights as a weighted directed graph and use a modified BFS (level-by-level) or Bellman-Ford to find the cheapest path with at most k stops. Since k is small, an iterative relaxation approach (Bellman-Ford) is efficient and easy to reason about. Alternatively, use Dijkstra with a state of (city, stops) but be careful with the stop constraint.
Pro tip: Clarify whether 'at most k stops' means the number of intermediate cities (stops) or the number of flights (edges). This off-by-one detail is a common pitfall; confirming it upfront shows attention to detail and avoids wasted effort.
Confirm the definition of 'stops' (intermediate cities vs. flights), whether costs are positive, and if multiple routes between same cities exist. Also ask about input size to choose the right algorithm.
Represent cities as nodes and flights as directed edges with weights (cost). This abstraction helps in applying standard graph algorithms.
For small k, use Bellman-Ford with at most k+1 iterations (since k stops means k+1 edges). Alternatively, use BFS with level tracking and keep track of minimum cost to each city at each stop count.
Initialize distances to infinity, set source distance to 0. Iterate k+1 times, relaxing all edges. After iterations, return distance to destination or -1 if unreachable. Handle cases like source equals destination (cost 0) and no flights.
Time complexity: O(k * E) for Bellman-Ford, where E is number of flights. Space: O(V). Mention that for large k, Dijkstra with state might be better, but for small k, this is optimal.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.