Code Room
CodingHardcod-g894
Subject Dijkstra variantsLevel Senior~35 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

You are given a directed graph on n nodes as edges [u, v, w] with positive integer weights, a source s, a target t, and an integer k. Return the length of the k-th shortest WALK from s to t, where walks may repeat nodes and edges and the same length may be achieved by different walks (each counted separately). Order walks by total length ascending; return the k-th value. Return -1 if fewer than k such walks exist. Constraints: up to 50 nodes; weights are positive (so no zero-cost cycles); k up to a few hundred.

Implement
kth_shortest(n: int, edges: list[list[int]], s: int, t: int, k: int) → int
Examples
in[3,[[0,1,1],[1,2,1],[1,1,2]],0,2,2]out4
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.