Minimum-Cost Meeting City
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure that carries this Google problem is a plain adjacency list, and the whole thing falls apart without it. Google reported this one in October 2024. You get an undirected, unweighted graph with up to 200000 nodes, two travelers, and a destination. You pick the meeting city that minimizes dist(x,m) + dist(y,m) + dist(m,destination). It's shortest-path dressed up as a meeting-point puzzle. If you blank on how to structure it, StealthCoder can run invisibly during the live OA as a safety net. Know the trick first, though. It's short.
The problem
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) → int Examples Example 1 n = 7 edges = [[0,2],[1,2],[2,3],[3,6],[0,4],[1,4],[4,5],[5,6]] x = 0 y = 1 destination = 6 return = 2 Meeting 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 = 4 edges = [[0,1],[2,3]] x = 0 y = 2 destination = 3 return = -1 The travelers start in different connected components, so no valid meeting city exists. Constraints 1 <= n <= 200000 0 <= edges.length <= 200000 Every row of edges is [u, v] with 0 <= u, v < n and u != v. 0 <= x, y, destination < n
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build an adjacency list from edges. Run BFS three times, from x, from y, and from destination. Because the graph is undirected and unweighted, BFS gives exact shortest distances in O(n + e). Then scan cities 0 to n-1 in order. Skip any city where one of the three distances is unreachable. Track the minimum sum, and only replace the best on a strictly smaller cost, so ties keep the smaller index automatically. The common pitfall is running BFS from every candidate city, which is O(n * (n + e)) and times out at 200000. Another one is forgetting that dist(m, destination) equals dist(destination, m) here because edges are undirected. Use -1 as the unreachable marker and don't add it into sums. If you freeze live, StealthCoder is the hedge that reads the problem and hands you this structure. Example 2 checks the disconnected case.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Minimum-Cost Meeting City cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum-Cost Meeting City FAQ
What's the trick in the Minimum-Cost Meeting City problem?+
Run BFS three times, from x, from y, and from destination. That gives every distance you need in linear time. Then loop over all cities and sum the three distances. Don't run a search per candidate city, that's far too slow for 200000 nodes.
Why BFS and not Dijkstra?+
The graph is unweighted, so BFS already returns shortest distances and runs in O(n + e). Dijkstra works but adds a heap and a log factor for no gain. Using it here just shows you didn't read the unweighted constraint.
How do I handle ties and unreachable cities?+
Iterate cities from 0 upward and only update the answer when the cost is strictly smaller. That keeps the smallest index on ties. If any of the three distances is unreachable for a city, skip it. If nothing survives, return -1.
Is this graph pattern still asked after Google's October 2024 report?+
Graph traversal with BFS is a staple, and this problem was reported by Google candidates in October 2024. Expect variants with multiple sources or a changed cost formula. The multi-source distance idea carries over, so it's worth knowing cold.
How do I prepare for this in 48 hours?+
Write BFS over an adjacency list from memory until it takes five minutes. Then practice combining several BFS distance arrays in one scan. Test the disconnected case from Example 2 and the tie case from Example 1. That covers nearly everything this problem tests.