Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Shortest Paths in Unweighted Graph

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

Your question is Shortest Paths in Unweighted 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 an unweighted graph with n nodes labeled from 0 to n - 1, an edge list edges, and a source node src, return an array dist where dist[i] is the length of the shortest path from src to node i. If a node is unreachable, return -1 for that node. The graph is undirected.

Constraints

  • 1 <= n <= 10^5
  • 0 <= edges.length <= 2 * 10^5
  • 0 <= u, v < n for each edge [u, v]
  • 0 <= src < n
  • The graph is unweighted and undirected

Function Signature

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