Code RoomSecond-shortest path distance
HardPrep Room Coding #822

Second-shortest path distance

CodingAlgorithms & data structuresSenior–Staff~40 min

Given an undirected weighted graph with n nodes (0..n-1) and edges [u, v, w] with positive weights, return the length of the strictly second-shortest path distance from a source to a destination. Walks may revisit nodes and edges, so the second-shortest value may be strictly larger than the shortest even when only one simple route exists. Return -1 if no second distinct distance exists. Constraints: 1 <= n <= 500.

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

0:00 of about 40 min
InputExpectedGot
[3,[[0,1,1],[1,2,1],[0,2,4]],0,2]4not run yetsample