Determine Whether Two Horses Are Genetically Related
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.
Examples
Example 1
horseIds = ["Nova","Comet","Blaze"]motherIds = ["","Nova",""]fatherIds = ["","",""]horseA = "Nova"horseB = "Comet"return = trueComet lists Nova as its mother, so one parent-child edge directly connects the queried horses.
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
$99 billed yearly — or $19 month-to-month. Cancel anytime.