FastPrepMinimum Shared Route Length for Two Travelers
Problems/Minimum Shared Route Length for Two Travelers

Minimum Shared Route Length for Two Travelers

Google logoGoogle● MediumNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand Google report from April 7, 2025 describes the task of the union of routes for two friends on an undirected, unweighted graph, and asks a three-friend follow-up. A report from July 2, 2025, for an L4 MLE role, says its phone-screen question was this same task and that the follow-up was discussed without code. Our version still uses two starting cities and one destination. The editorial explains the three-start variant without assuming that all travelers meet in one city. The input limits and -1 for unreachable are our choice.

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) → int

Examples

Example 1

n = 5edges = [[0,2],[1,2],[2,3],[3,4]]a = 0b = 1destination = 4return = 4

The 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 = 2

Each traveler can use one direct edge to the destination.

Example 3

n = 4edges = [[0,1],[2,3]]a = 0b = 2destination = 3return = -1

The 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, and destination are 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

Interview reports that mention this question

  • Google L4 ML Loops and Team Matching: 8 Experiences (Part 2 of 4)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int minimumSharedRouteLength(int n, int[][] edges, int a, int b, int destination) {
        // Write your code here.
    }
}
Checking account…