It's basically lowest common ancestor but reframed as a genealogy problem.
Model the horse family tree as a graph where nodes are horses and edges represent parent-child relationships, then determine if there is a path between the two horses. Use BFS/DFS from one horse to find the other, or compute the lowest common ancestor (LCA) if the tree is rooted. Clarify whether 'blood relation' means any shared ancestor or a direct lineage, and handle potential cycles or multiple parents.
Pro tip: Always clarify the definition of 'blood relation' with the interviewer—whether it includes indirect relations (e.g., cousins) or only direct ancestors/descendants—as this significantly impacts the algorithm and shows attention to detail.
Ask the interviewer to define 'blood relation' (e.g., any shared ancestor vs. direct lineage) and confirm the data structure (tree, graph, multiple parents).
Represent the genealogy as a graph (adjacency list) or tree (parent pointers). If it's a tree, identify the root; if not, treat as an undirected graph.
For general graphs, use BFS/DFS to check connectivity. For trees, compute LCA or traverse ancestors. Consider time/space complexity.
Account for cycles, multiple parents, disconnected components, and the horses being the same. Discuss how to handle large datasets.
If needed, preprocess the tree for LCA (e.g., binary lifting) to answer queries in O(log n). Compare with BFS/DFS for single query.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.