Minimum-Cost Meeting City
Updated
Problem statement
An undirected, unweighted graph represents n cities numbered from 0 to n - 1. Travelers start at cities x and y. They may travel separately to a meeting city m, then ride together from m to destination.
The cost of choosing m is dist(x, m) + dist(y, m) + dist(m, destination), so the shared final route is counted once.
Return the meeting city with minimum cost. If several cities have the same minimum cost, return the smallest city index. Return -1 if no city is reachable from both travelers and can reach destination.
Function
minimumCostMeetingCity(n: int, edges: int[][], x: int, y: int, destination: int) → intExamples
Example 1
n = 7edges = [[0,2],[1,2],[2,3],[3,6],[0,4],[1,4],[4,5],[5,6]]x = 0y = 1destination = 6return = 2Meeting at city 2 costs 1 + 1 + 2 = 4. Meeting at city 4 also costs 4, so the smaller city index 2 wins the tie.
Example 2
n = 4edges = [[0,1],[2,3]]x = 0y = 2destination = 3return = -1The travelers start in different connected components, so no valid meeting city exists.
Constraints
1 <= n <= 2000000 <= edges.length <= 200000- Every row of
edgesis[u, v]with0 <= u, v < nandu != v. 0 <= x, y, destination < n
More Google Phone Screen questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE 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
- Logger Rate Limiter with Every-Arrival HistoryPHONE SCREEN · 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