Code Room
CodingHardcod-g282
Subject Shortest pathLevel Senior–Staff~35 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Given a weighted directed graph on n nodes (edges [u, v, w] with w >= 1), a source src, a destination dst, and an integer k, return the length of the k-th shortest path from src to dst by total weight. Paths may revisit nodes (so multiple distinct-length routes through cycles count), and the k smallest path lengths are taken in non-decreasing order (with multiplicity). Return -1 if fewer than k distinct paths exist / dst is unreachable in k arrivals.

Implement
kth_shortest(n: int, edges: list[list[int]], src: int, dst: int, k: int) → int
Examples
in[4,[[0,1,1],[0,2,4],[1,2,2],[2,3,1],[1,3,5]],0,3,2]out5
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.