FastPrepPeople with Zero or One Parent
Problems/People with Zero or One Parent

People with Zero or One Parent

Indeed logoIndeed● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The first part of a Karat family-tree question asks which people have no parent or just one.

The report writes each pair as (child, parent). A fuller Karat report about the same family data writes (parent, child), and this version uses that order. The report does not say how to return the two kinds of node, and it gives no limits. Here, you return two lists, people with zero parents and people with exactly one parent, each in increasing order. Only people who appear in the pairs count. Pairs never repeat, the family has no cycles, and the size limits are practice choices.

The judged task matches the report at about 80%.

You are given data about parents and children. Each entry of parentChildPairs is [parent, child], and every person has a unique integer ID. A person can have any number of parents in the data.

Consider every person who appears in parentChildPairs, as a parent, as a child, or both. Return two lists:

  1. The people with zero parents in the data.
  2. The people with exactly one parent in the data.

Return them as [zeroParents, oneParent], each sorted in increasing order. A list with nobody in it is empty.

Function

parentGroups(parentChildPairs: int[][]) → int[][]

Examples

Example 1

parentChildPairs = [[10,2],[11,2],[2,7],[12,7],[12,9],[9,4]]return = [[10,11,12],[4,9]]

Nobody lists 10, 11 or 12 as a child, so they have zero parents. 9 has the single parent 12, and 4 has the single parent 9. 2 has two parents (10 and 11), and so does 7 (2 and 12), so they are in neither list.

Example 2

parentChildPairs = [[3,1],[4,1]]return = [[3,4],[]]

3 and 4 have no parents. 1 has two parents, so nobody has exactly one parent and the second list is empty.

Example 3

parentChildPairs = [[5,8],[8,6]]return = [[5],[6,8]]

5 has no parent. 8 has the single parent 5, and 6 has the single parent 8. Each list is in increasing order, so 6 comes before 8.

Constraints

  • 1 <= parentChildPairs.length <= 2000
  • parentChildPairs[i].length == 2
  • 1 <= parentChildPairs[i][j] <= 10^5
  • parentChildPairs[i][0] != parentChildPairs[i][1], and no pair appears twice.
  • The pairs have no cycle: nobody is their own ancestor.

More Indeed problems

  • Insert into an Unrolled Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Course Completion OrderPHONE SCREEN · Seen May 2025
  • Recipes You Can PreparePHONE SCREEN · Seen May 2025
All Indeed interview questionsSee Indeed hiring insights
Saved
class Solution {
    public int[][] parentGroups(int[][] parentChildPairs) {
        // Write your code here.
    }
}
Checking account…