← Questions
Fastest trip when two travellers may meet and ride together
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked Jan 2026. It tests graphs, bfs and shortest paths.
Two people start at different nodes of a weighted undirected graph and both need to reach the same destination. They may travel separately, or meet at some node and ride together from there. A stretch they ride together is counted once. Return the smallest total travel time: the time of each person's separate legs plus the shared leg.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
best_trip(n: int, edges: list[list[int]], a: int, b: int, dest: int) -> int— nodes0..n-1, each edge[u, v, time]; returns-1if the destination cannot be reached by both. Travelling separately is the case where they "meet" at the destination.
Example
n = 4, edges = [[0,2,2], [1,2,2], [2,3,6], [0,3,9], [1,3,9]], a = 0, b = 1, dest = 3
-> 10 # both go to node 2 (2 + 2), then ride together to 3 (6)
# travelling separately costs 8 + 8 = 16 (each goes via node 2 alone)
Constraints
- Up to 10^5 nodes and edges; times are non-negative.
Clarifying questions
- Is the cost total time across both people, or the time until both arrive?
- Does the shared ride cost the same per edge as travelling alone?
- Must they arrive together? Can one wait for the other at the meeting point?
- Is the graph directed?
What a strong answer covers
- The key reduction: for any meeting node
m, the cost isdist(a, m) + dist(b, m) + dist(m, dest), so the answer is the minimum over allm. - Three Dijkstra runs, from
a, fromband fromdest(on the reversed graph if edges are directed), then one pass over the nodes. O((V + E) log V). - Why trying every pair of paths, or a Dijkstra from each node, is too slow.
- Unreachable nodes,
a == b, and the meeting node equal to a start or the destination.
Follow-ups
- They move at the same time and the shared vehicle is twice as fast: minimize the time until both arrive,
max(dist(a, m), dist(b, m)) + dist(m, dest) / 2. - Three or more travellers.
- Return the meeting node and both routes.
Where it's asked
- Google · First round: coding · Last asked Jan 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
