Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Shortest Path in Weighted Graph

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Shortest Path in Weighted Graph. Start with the requirements on the right.

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.

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output