Find people with zero or one parent and check for a common ancestor

Quick Overview

Given parent-child pairs from a family tree, list the people with no parents and the people with exactly one parent, then decide whether two given people share an ancestor. It tests building a reverse adjacency map, counting parents per node, and walking a directed acyclic graph upward with a visited set.

Find people with zero or one parent and check for a common ancestor

Company: Compass

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of parent-child `pairs` that describes a family tree. Each pair `[parent, child]` means that `parent` is a direct parent of `child`. Implement the following: - Find all individuals who have zero parents. - Find all individuals who have exactly one parent. - Given two individuals `ind1` and `ind2`, determine whether they share at least one common ancestor. An ancestor is a parent, grandparent, great-grandparent, and so on. Return a list of three elements, `[zero_parents, one_parent, has_common_ancestor]`, where `zero_parents` is a sorted list of the individuals with no parents, `one_parent` is a sorted list of the individuals with exactly one parent, and `has_common_ancestor` is a boolean that says whether the two given individuals share a common ancestor. ### Function Signature ```python def find_family_info(pairs: list[list[int]], ind1: int, ind2: int) -> list: ... ``` ### Rules - The individuals are exactly the values that appear in at least one pair, as a parent or as a child. - Each `[parent, child]` pair appears at most once in `pairs`. - Both lists are sorted in ascending order. People with two or more parents appear in neither list. - `has_common_ancestor` is `True` when some person is an ancestor of both `ind1` and `ind2`. A person is not their own ancestor, so `ind1` being a parent or grandparent of `ind2` does not by itself make the answer `True`. ### Constraints - `1 <= len(pairs) <= 1000` - Each pair is `[parent, child]`, where `parent` and `child` are positive integers. - `1 <= individual value <= 10000` - `ind1` and `ind2` are guaranteed to appear in the family tree. - The parent-child graph has no cycles. ### Examples ```text Input: pairs = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[8,9]], ind1 = 3, ind2 = 8 Output: [[1, 2, 4], [5, 7, 8, 9], False] ``` Individuals 1, 2 and 4 never appear as a child, 5, 7, 8 and 9 have one parent each, and 3 and 6 have two parents each. The ancestors of 3 are {1, 2} and the ancestors of 8 are {4}, which do not overlap. ```text Input: pairs = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[8,9]], ind1 = 6, ind2 = 7 Output: [[1, 2, 4], [5, 7, 8, 9], True] ``` The ancestors of 6 are {1, 2, 3, 4, 5} and the ancestors of 7 are {4, 5}, so they share 4 and 5. ```text Input: pairs = [[1,2],[2,3]], ind1 = 1, ind2 = 3 Output: [[1], [2, 3], False] ``` Individual 1 is an ancestor of 3, but 1 has no ancestors of its own and does not count as its own ancestor, so the two share none.

Overview: Given parent-child pairs from a family tree, list the people with no parents and the people with exactly one parent, then decide whether two given people share an ancestor. It tests building a reverse adjacency map, counting parents per node, and walking a directed acyclic graph upward with a visited set.

Compass logo
Compass
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of parent-child pairs that describes a family tree. Each pair [parent, child] means that parent is a direct parent of child. Implement the following:

  • Find all individuals who have zero parents.
  • Find all individuals who have exactly one parent.
  • Given two individuals ind1 and ind2 , determine whether they share at least one common ancestor. An ancestor is a parent, grandparent, great-grandparent, and so on.

Return a list of three elements, [zero_parents, one_parent, has_common_ancestor], where zero_parents is a sorted list of the individuals with no parents, one_parent is a sorted list of the individuals with exactly one parent, and has_common_ancestor is a boolean that says whether the two given individuals share a common ancestor.

Function Signature

def find_family_info(pairs: list[list[int]], ind1: int, ind2: int) -> list:
    ...

Rules

  • The individuals are exactly the values that appear in at least one pair, as a parent or as a child.
  • Each [parent, child] pair appears at most once in pairs .
  • Both lists are sorted in ascending order. People with two or more parents appear in neither list.
  • has_common_ancestor is True when some person is an ancestor of both ind1 and ind2 . A person is not their own ancestor, so ind1 being a parent or grandparent of ind2 does not by itself make the answer True .

Constraints

  • 1 <= len(pairs) <= 1000
  • Each pair is [parent, child] , where parent and child are positive integers.
  • 1 <= individual value <= 10000
  • ind1 and ind2 are guaranteed to appear in the family tree.
  • The parent-child graph has no cycles.

Examples

Input: pairs = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[8,9]], ind1 = 3, ind2 = 8
Output: [[1, 2, 4], [5, 7, 8, 9], False]

Individuals 1, 2 and 4 never appear as a child, 5, 7, 8 and 9 have one parent each, and 3 and 6 have two parents each. The ancestors of 3 are {1, 2} and the ancestors of 8 are {4}, which do not overlap.

Input: pairs = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[8,9]], ind1 = 6, ind2 = 7
Output: [[1, 2, 4], [5, 7, 8, 9], True]

The ancestors of 6 are {1, 2, 3, 4, 5} and the ancestors of 7 are {4, 5}, so they share 4 and 5.

Input: pairs = [[1,2],[2,3]], ind1 = 1, ind2 = 3
Output: [[1], [2, 3], False]

Individual 1 is an ancestor of 3, but 1 has no ancestors of its own and does not count as its own ancestor, so the two share none.