Shortest Path With K Free Edges
Reported by candidates from Zorvyn's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Zorvyn reportedly served this one in April 2026, and the trap is built into the title. Shortest path with k free edges looks like plain Dijkstra with a coupon. It isn't. If you run normal Dijkstra over nodes alone, you lose track of how many bridges you've spent, and your answer breaks the moment k matters. This is a graph problem where the state is bigger than the node. If you've got the OA in a day or two, learn the state shape now. StealthCoder is there as a safety net if you blank during the live assessment, but the pattern below is short enough to own.
The problem
You are given an undirected weighted graph with n nodes labeled from 1 to n. The normal weighted edges are given in edges, where each edge is [u, v, w]. You are also given a list of special edges called magical bridges. Each bridge is given as [u, v] and can be traversed with cost 0. You may use at most k magical bridge traversals in total. Return the minimum cost required to travel from node 1 to node n. If it is not possible, return -1. Function shortestPathWithKFreeEdges(n: int, edges: int[][], bridges: int[][], k: int) → long Examples Example 1 n = 4 edges = [[1, 2, 10], [2, 4, 10], [1, 3, 5], [3, 4, 20]] bridges = [[1, 4]] k = 1 return = 0 Use the magical bridge directly from node 1 to node 4 with cost 0. Example 2 n = 3 edges = [[1, 2, 5]] bridges = [] k = 1 return = -1 Node 3 cannot be reached. Constraints The graph is undirected. edges[i] = [u, v, w] describes a weighted edge. bridges[i] = [u, v] describes a zero-cost magical bridge. 0 <= k
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a layered state: (node, bridgesUsed). Run Dijkstra on that pair with a min-heap. From (u, j), a normal edge goes to (v, j) with cost w. A bridge goes to (v, j+1) with cost 0, only if j < k. Bridges are undirected like the rest of the graph, so add them both ways. Your distance table is n by (k+1). The answer is the minimum over all j of dist[n][j], or -1 if every entry is infinite. The common pitfall is keeping a single dist per node and pruning states that are worse in cost but better in bridges left. That prunes valid paths. Another miss is using int when the return type is long. Also handle k = 0, where bridges simply never get used. If the live OA freezes you on the state design, StealthCoder can supply the layered Dijkstra while you stay calm.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Shortest Path With K Free Edges 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as cheapest flights within k stops. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Zorvyn's OA.
Zorvyn reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Path With K Free Edges FAQ
What's the trick in Shortest Path With K Free Edges?+
Add the number of bridges used to the state. Run Dijkstra over (node, used) pairs instead of nodes alone. Normal edges keep used the same, bridges bump it by one at zero cost. Take the minimum distance to node n across all used counts from 0 to k.
Why does plain Dijkstra fail here?+
A single distance per node forgets how many free bridges you've spent. A slightly costlier path that saved bridges can beat a cheaper one that burned them. Pruning by node alone throws away that path and gives wrong answers on cases where k is limited.
How hard is this really?+
Medium. If you know Dijkstra, the only new idea is the extra dimension. Complexity is roughly (n + m) times (k + 1) times a log factor. Most people lose points on state handling and edge cases, not on the algorithm itself.
What edge cases should I test before submitting?+
Test k = 0, an empty bridges list, and a disconnected node n, which should return -1. Also test when n equals 1 if allowed, where the cost is 0. Use a 64-bit type for distances, since the return is long and weights can add up.
How do I prepare in 48 hours?+
Write Dijkstra with a heap from scratch twice. Then extend it to a layered state by adding one dimension. Code the bridge transition as a zero-cost move to layer j+1. Run both examples by hand. That covers this problem and most layered-graph variants.