Shortest Distance in a Graph
Updated
Problem statement
You are given an undirected, unweighted graph with n vertices labeled from 0 through n - 1. Each pair [u, v] in edges connects vertices u and v in both directions.
Return the minimum number of edges in a path from start to target. Return -1 if no such path exists.
- Every edge takes one step to traverse.
- If
startequalstarget, return0. - The graph may be disconnected or contain cycles. Duplicate edges and self-loops are allowed; they do not create a shorter route.
- Return only the minimum distance, not the sequence of vertices.
Function
shortestDistance(n: int, edges: int[][], start: int, target: int) → intExamples
Example 1
n = 6edges = [[0,1],[1,2],[2,5],[0,3],[3,5],[3,4]]start = 0target = 5return = 2The path 0 -> 3 -> 5 uses two edges. No edge connects 0 directly to 5, so a one-edge path is impossible.
Example 2
n = 5edges = [[0,1],[1,2],[3,4]]start = 0target = 4return = -1Vertices 0 and 4 belong to different connected components, so the result is -1.
Example 3
n = 3edges = [[0,1]]start = 2target = 2return = 0The start is already the target. No edge needs to be traversed, even though vertex 2 is isolated.
Constraints
1 <= n <= 200000 <= edges.length <= 100000- Every edge contains exactly two vertex labels.
- For every edge
[u, v],0 <= u, v < n. 0 <= start, target < n
More Zip Phone Screen questions
- Maximum Survival Time in a Graph PursuitPHONE SCREEN · Seen Sep 2026
- Find All Zip Rummy MeldsPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2026
- Shortest Path Around Moving ParadesPHONE SCREEN · Seen Mar 2026
- Furthest Building with Sandbags and RopesPHONE SCREEN · Seen Feb 2026
- Phrase Covering the Most WordsPHONE SCREEN · Seen Feb 2026
- Translate Values in a Nested MapPHONE SCREEN · Seen Feb 2026
- Maximum Nonadjacent Sum in a General TreePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2025
- Open the Lock and Return a Shortest PathPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2025
- Winning Draws for a Mahjong HandPHONE SCREEN · Seen Sep 2025
- Find Root IDs from Parent ReferencesPHONE SCREEN · Seen Aug 2025
Similar questions at other companies
- Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
- Cheapest Flights Within K StopsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Shortest Word Chain Between Two WordsFanatics · PHONE SCREEN
- Shortest Path Node Count Avoiding Broken NodesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Shortest Distances From an Adjacency MatrixBoston Consulting Group · PHONE SCREENBoston Consulting Group interview questions
- Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Find a Directed Resource Access PathCoreWeave · PHONE SCREEN
- Fire Station Response BillLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Delivery Management SystemAkuna Capital · OAAkuna Capital interview questions
- Minimum Friend API CallsMercor · ONSITE INTERVIEWMercor interview questions