Minimum Shared Route Length for Two Travelers
Updated
Problem statement
An undirected, unweighted graph represents n cities numbered from 0 to n - 1. Two travelers start at cities a and b, and both want to reach city destination.
They may travel separately, or meet at any city and share the remainder of the route. A shared edge is counted only once. Return the minimum possible number of distinct route edges used by the two travelers together. Return -1 if no valid pair of routes exists.
Function
minimumSharedRouteLength(n: int, edges: int[][], a: int, b: int, destination: int) → intExamples
Example 1
n = 5edges = [[0,2],[1,2],[2,3],[3,4]]a = 0b = 1destination = 4return = 4The travelers meet at city two and share the final two edges.
Example 2
n = 4edges = [[0,3],[1,3],[0,2],[1,2],[2,3]]a = 0b = 1destination = 3return = 2Each traveler can use one direct edge to the destination.
Example 3
n = 4edges = [[0,1],[2,3]]a = 0b = 2destination = 3return = -1The first traveler cannot reach the destination or any meeting city reachable by the second traveler.
Constraints
1 <= n <= 200000.0 <= edges.length <= 300000.- Every edge contains two distinct valid city IDs.
a,b, anddestinationare valid city IDs.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
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
- Maximum Survival Time in a Graph PursuitZip · PHONE SCREENZip 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