Code Room
CodingMediumcod-g006
Subject Shortest pathLevel Mid–Senior~35 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Given a directed weighted graph with n nodes (0..n-1) and edges as [u, v, w] with w >= 0, return the length of the shortest path from node src to node dst. If dst is unreachable from src, return -1. There may be multiple edges between the same pair; use the one giving the shorter total. Self-loops may appear and should be ignored as they never help.

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