Code Room
CodingHardcod-g701
Subject Dijkstra variantsLevel Senior–Staff~40 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

A directed graph on n nodes (0..n-1) has edges [u, v, w] with positive weights. Return the length of the SECOND shortest path from s to t, where 'second shortest' is the smallest path length strictly greater than the shortest path length. Paths may revisit nodes (they are walks), so a second-shortest can reuse vertices. Return -1 if no such second distinct length exists (or t is unreachable). Constraints: 1 <= n <= 2000; 1 <= w <= 10^6.

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