Nodes With Zero or Three Parents
Updated
Problem statement
Each row of pairs is [parent,child] and describes one directed parent-child relationship. Return all represented nodes with no direct parents and all represented nodes with exactly three distinct direct parents. Count direct parents, rather than all ancestors.
For this exercise, assume the relationships form a directed acyclic graph that permits multiple parents. Repeated copies of the same pair count as one relationship.
Return exactly two rows: [roots,nodesWithThreeParents]. For this exercise, assume each row is sorted by increasing numeric ID; only nodes appearing as a parent or child are represented, and empty input returns [[],[]].
Function
getRootAndNodesWithThreeParents(pairs: int[][]) → int[][]Examples
Example 1
pairs = [[1,9],[2,9],[3,9],[9,10]]return = [[1,2,3],[9]]Nodes 1, 2, 3 have no parents. Node 9 has exactly three distinct direct parents; node 10 has only one and is excluded from the second row.
Example 2
pairs = [[4,1],[4,2],[2,3]]return = [[4],[]]Only direct parents are counted. Node 4 is a root; the remaining nodes each have one direct parent.
Constraints
- For this exercise, assume
0 <= pairs.length <= 1000. - Every row contains exactly two positive integer IDs, each at most
10^9. - The directed relationships contain no cycles; duplicate pairs may appear.
More Compass International Holdings Phone Screen questions
- Decode StringPHONE SCREEN · Seen Oct 2026
- Lowest Common Ancestors in a Parent DAGPHONE SCREEN · Seen Apr 2022
- Can One Dictionary Word Supply the Target Letters?PHONE SCREEN · Seen Apr 2022
- Greedy Grid Word Occurrences with No Reused CellsPHONE SCREEN · Seen Apr 2022
- Word Path Moving Only Right or DownPHONE SCREEN · Seen Apr 2022
- First Dictionary Word from Note LettersPHONE SCREEN · Seen Mar 2022
- Word Path Moving Only Up or RightPHONE SCREEN · Seen Feb 2022
- Top Five Stock Records in a Date RangePHONE SCREEN · Seen Oct 2021
- Wrap Words from Existing Text LinesPHONE SCREEN · Seen Apr 2021
- All Common Ancestors in a Parent GraphPHONE SCREEN · Seen Jan 2021
Similar questions at other companies
- Rooms With Two Other Entrances and a Treasure SuccessorWalmart · PHONE SCREENWalmart interview questions
- Parent and Child Counts for Every NodeKarat · PHONE SCREENKarat interview questions
- People with Zero or One ParentIndeed · PHONE SCREENIndeed interview questions
- Mutual-Friend Referral RecommendationsFireworks AI · PHONE SCREEN · ONSITE INTERVIEW
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Top K Users by Distinct ContactsGoogle · PHONE SCREENGoogle interview questions
- Assign Campers to the First Arriving CarAtlassian · PHONE SCREENAtlassian interview questions
- Resource Transition ProbabilitiesWhatNot · PHONE SCREENWhatNot interview questions
- Find Every Reachable Derived Vocabulary WordDuolingo · PHONE SCREENDuolingo interview questions
- Order Employees by a Reports-To HierarchyBloomberg LP · PHONE SCREENBloomberg LP interview questions