Code Room
CodingMediumcod-g468
Subject Dijkstra variantsLevel Mid–Senior~25 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Given a directed weighted graph with n nodes (0..n-1), edges [u, v, w] (non-negative weights), a source src, a destination dst, and an integer K, return the minimum total weight of a path from src to dst that uses AT MOST K edges, or -1 if no such path exists. K may be 0 (then only src reaches itself). Assume 1 <= n <= 500 and K <= n; a layered Bellman-Ford-style relaxation (not plain Dijkstra) is the intended approach.

Implement
shortest_path_max_edges(n: int, edges: list[list[int]], src: int, dst: int, K: int) → int
Examples
in[4,[[0,1,100],[1,2,100],[0,2,500],[2,3,100]],0,3,2]out600
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.