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

Shortest Path in Review Graph

EasyPython

Problem

Given an undirected graph with n nodes labeled 0 to n - 1, an edge list edges, a starting node start, and a target node target, return the length of the shortest path from start to target. If no path exists, return -1. Implement an algorithm that works efficiently for large sparse graphs.

Constraints

  • 1 <= n <= 10^5
  • 0 <= edges.length <= 2 * 10^5
  • edges[i].length == 2
  • 0 <= edges[i][0], edges[i][1] < n
  • 0 <= start, target < n
  • The graph may be disconnected and may contain duplicate edges

Function Signature

def shortest_path_length(n, edges, start, target):
Interviewer

Your question is Shortest Path in Review 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.