Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Shortest Path in Weighted Graph
00:00
5 left

Shortest Path in Weighted Graph

HardPython

Problem

Given a weighted directed graph with n nodes labeled from 0 to n - 1, an edge list edges where each element is [u, v, w], a source node src, and a destination node dst, return the length of the shortest path from src to dst. If no path exists, return -1. All edge weights are non-negative integers.

Constraints

  • 1 <= n <= 10^5
  • 0 <= edges.length <= 2 * 10^5
  • 0 <= u, v < n
  • 0 <= w <= 10^9
  • 0 <= src, dst < n
  • All edge weights are non-negative

Function Signature

def shortest_path(n, edges, src, dst):
Interviewer

Your question is Shortest Path in Weighted Graph. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.