Reported September 2026
Amazonshortest path

Cheapest Flights Within K Stops

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The Amazon OA reported in September 2026 is Cheapest Flights Within K Stops, and the whole problem hinges on one choice: how you track cost per number of stops. It's a shortest-path problem with a leash. Plain Dijkstra will bite you here, because the cheapest route to a city isn't always the one you want if it burned too many stops. You've got a graph of up to 100 cities, a source, a destination, and a stop limit. If your mind goes blank on the stop constraint, StealthCoder sits invisibly on your screen as a safety net during the live OA. Know the trick first, though.

The problem

There are n cities numbered from 0 to n - 1. You are given an array flights where flights[i] = [from_i, to_i, price_i] means there is a directed flight from city from_i to city to_i with cost price_i.
You are also given integers src, dst, and k. Return the cheapest price from src to dst with at most k stops. If no such route exists, return -1.

Function
findCheapestPrice(n: int, flights: int[][], src: int, dst: int, k: int) → int

Examples
Example 1
n = 4
flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]]
src = 0
dst = 3
k = 1
return = 700
The route 0 -> 1 -> 3 costs 700 and uses 1 stop. The cheaper route 0 -> 1 -> 2 -> 3 costs 400 but uses 2 stops, which exceeds k.
Example 2
n = 3
flights = [[0,1,100],[1,2,100],[0,2,500]]
src = 0
dst = 2
k = 1
return = 200
The route 0 -> 1 -> 2 costs 200 with 1 stop, which is cheaper than the direct flight of 500.

Constraints
1 <= n <= 100.
0 <= flights.length <= n * (n - 1).
flights[i].length == 3.
0 <= from_i, to_i < n.
from_i != to_i.
1 <= price_i <= 10^4.
There are no duplicate flights and no self-flights.
0 <= src, dst, k < n.
src != dst.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The cleanest answer is Bellman-Ford limited to k+1 rounds. Keep a costs array, set src to 0 and everything else to infinity. For each round, copy the array, then relax every flight using the previous round's values only. The copy matters. Without it, one round can chain several flights and you silently break the stop limit. After k+1 rounds, return costs[dst] or -1 if it's still infinity. The alternative is BFS by level or a heap storing (cost, city, stops), but you must allow revisiting a city when it arrives with fewer stops. The common pitfall is marking cities visited after the first cheapest arrival, which gives wrong answers on Example 1. Off-by-one is the other trap: k stops means k+1 edges. If you freeze on that during the live OA, StealthCoder can hand you the working version as a hedge.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Cheapest Flights Within K Stops cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as cheapest flights within k stops. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Cheapest Flights Within K Stops FAQ

How hard is Cheapest Flights Within K Stops really?+

Medium. The graph is small, with n at most 100, so brute-force-ish approaches pass. The difficulty is realizing standard Dijkstra fails because of the stop limit. Once you see that, Bellman-Ford with k+1 rounds is about fifteen lines of code.

What's the trick to this problem?+

Bound the number of edges, not just the cost. Run k+1 relaxation rounds, and in each round read from a copy of the previous round's costs. That copy guarantees each round adds at most one flight to any path.

Why does plain Dijkstra give wrong answers here?+

Dijkstra finalizes a city at its cheapest cost, ignoring stops. In Example 1, the 400 route to city 3 uses 2 stops and is invalid, so the answer is 700. A cheap path with too many stops can block a pricier valid one.

Is k stops the same as k edges?+

No. k stops means at most k+1 flights, since the stops are the cities between src and dst. Most wrong submissions come from looping k times instead of k+1. Check it against Example 2, where k = 1 allows two flights.

How do I prepare for this in 48 hours?+

Write Bellman-Ford with the copied array from memory, then write the BFS or heap version that tracks stops. Test both on the two examples. Also practice the unreachable case returning -1, since Amazon-style tests usually include it.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it