FastPrepMinimum Union of Two Routes

Minimum Union of Two Routes

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Problem statement

You are given an undirected, unweighted graph with n vertices numbered from 0 to n - 1. Alice starts at vertex a, Bob starts at vertex b, and both must reach destination d.

Alice chooses one path from a to d, and Bob chooses one path from b to d. The cost of their two routes is the number of distinct undirected edges that appear in the union of both paths.

Return the minimum possible cost. Return -1 when either traveler cannot reach d.

The problem statement continues
Pro

Examples

Example 1

n = 7edges = [[0,2],[1,2],[2,3],[3,6],[0,4],[1,4],[4,5],[5,6]]a = 0b = 1d = 6return = 4

The travelers can meet at vertex 2. The edges from 0 and 1 to 2 contribute two edges, and the shared suffix 2 - 3 - 6 contributes two more.

FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week
See Google hiring insights
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week