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

Question

You are given a directed acyclic graph (DAG) on n nodes as edges [u, v, w] with positive integer weights, plus a source s and target t. Return the length of the SECOND smallest distinct path length from s to t (the next value strictly greater than the shortest-path length, considering all distinct path-length values). Return -1 if fewer than two distinct path lengths exist. Constraints: the graph is a DAG; up to 60 nodes; parallel edges allowed.

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