FastPrepDetermine Whether Two Horses Are Genetically Related
Problem · Graph

Determine Whether Two Horses Are Genetically Related

MediumGoogle logoGoogleNEW GRADINTERNONSITE INTERVIEW
See Google hiring insights

Problem statement

The International Horse Racing Committee maintains birth-certificate data for a collection of horses. Each record identifies a horse and, when known, its mother and father.

Each position i describes one horse:

  • horseIds[i] is the horse's identifier.
  • motherIds[i] is its mother's identifier, or the empty string "" when the mother is unknown.
  • fatherIds[i] is its father's identifier, or the empty string "" when the father is unknown.

Relationship rule

For this exercise, assume every non-empty parent entry creates an undirected relationship edge between the horse and that parent. Two horses are genetically related when a path of one or more such parent-child edges connects them. A horse is also related to itself.

The problem statement continues
Pro

Examples

Example 1

horseIds = ["Nova","Comet","Blaze"]motherIds = ["","Nova",""]fatherIds = ["","",""]horseA = "Nova"horseB = "Comet"return = true

Comet lists Nova as its mother, so one parent-child edge directly connects the queried horses.

Original screenshot from the interview report
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
  • 3 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
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
  • 3 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