Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Shortest Path and Components

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

Your question is Shortest Path and Components. 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 an undirected unweighted graph with n nodes labeled from 0 to n - 1 and an edge list edges, implement a function that returns two results: the length of the shortest path from start to end, and the number of connected components in the graph. If no path exists from start to end, return -1 for the shortest path length.

Constraints

  • 1 <= n <= 10^5
  • 0 <= edges.length <= 2 * 10^5
  • 0 <= start, end < n
  • Each edge is a pair [u, v] with u != v
  • The graph is undirected and unweighted

Function Signature

def analyze_graph(n, edges, start, end):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output