Nodes With Zero or One Parent
Updated
Problem statement
Each row of pairs is [parent,child]. Return the represented nodes with zero direct parents and those with exactly one distinct direct parent.
For this exercise, assume a directed acyclic graph that permits several parents; repeated copies of a pair count as one relationship. Only IDs appearing in pairs are represented.
For this exercise, assume exactly two rows [roots,nodesWithOneParent], each in increasing numeric order. Empty input returns [[],[]].
Function
findParentGroups(pairs: int[][]) → int[][]Examples
Example 1
pairs = [[10,30],[20,30],[40,20],[40,70]]return = [[10,40],[20,70]]This graph has roots 10 and 40, with one-parent nodes 20 and 70. Node 30 has two parents.
Example 2
pairs = [[5,2],[2,9]]return = [[5],[2,9]]These groups count direct parents rather than all preceding generations.
Constraints
- For this exercise, assume
0 <= pairs.length <= 1000. - Each row contains two positive integer IDs, each at most
10^9. - The directed relationships contain no cycles.
More Karat interview questions
- Do Two Nodes Share a Proper Ancestor?PHONE SCREEN · Seen Sep 2021
- Parent and Child Counts for Every NodePHONE SCREEN · Seen Sep 2021
- Validate a Latin-Square MatrixPHONE SCREEN · Seen Aug 2020
- Validate a Nonogram GridPHONE SCREEN · Seen Aug 2020
- Lowest Common Ancestor in a Rooted TreeONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Network Endpoint or Cycle BoundaryOpenAI · OA · PHONE SCREENOpenAI interview questions
- Most Active Caller by Distinct ContactsGoogle · PHONE SCREENGoogle interview questions
- Initial and Final Accounts in a Transfer ChainAmazon · PHONE SCREEN
- Find Root IDs from Parent ReferencesZip · PHONE SCREENZip interview questions
- Rooms With Two Other Entrances and a Treasure SuccessorWalmart · PHONE SCREENWalmart interview questions
- Middle Course of a Course TrackWhatNot · PHONE SCREENWhatNot interview questions
- People with Zero or One ParentIndeed · PHONE SCREENIndeed interview questions
- Nodes With Zero or Three ParentsCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Start and End of a Trip From Shuffled TicketsNutanix · PHONE SCREENNutanix interview questions