Tree Nodes with Zero or One Parent
Updated
Problem statement
Each row of pairs is [parent,child]. Return the represented nodes with no direct parents and those with exactly one direct parent. Count immediate parents rather than every ancestor.
For this exercise, assume the unique relationships form one connected rooted tree when nonempty: they contain no cycles, and each non-root node has exactly one parent. Repeated copies of a pair have no additional effect. Only IDs appearing in a pair are represented.
For this exercise, assume the result is exactly [roots,nodesWithOneParent], with both rows sorted by increasing numeric ID. Empty pairs represent an empty tree and return [[],[]]. Do not change the input.
Function
findTreeParentGroups(pairs: int[][]) → int[][]Examples
Example 1
pairs = [[8,2],[8,9],[2,5]]return = [[8],[2,5,9]]Node 8 is the root. Nodes 2,5,9 each have one direct parent, even though node 5 has two ancestors.
Example 2
pairs = [[4,1],[4,1],[1,7]]return = [[4],[1,7]]The repeated 4-to-1 record is one relationship. Node 4 has no parent; nodes 1 and 7 each have one.
Example 3
pairs = []return = [[],[]]No represented nodes give two empty groups.
Constraints
- For this exercise, assume
0 <= pairs.length <= 1000. - Each row has exactly two positive integer IDs, each at most
10^9. - After duplicate pairs are ignored, nonempty input forms one connected rooted tree. Separate isolated nodes are not encoded by this interface.
More Wayfair interview questions
- Container With Most WaterPHONE SCREEN · Seen Sep 2026
- Assign Distinct Letter Values to Minimize XORPHONE SCREEN · Seen Sep 2026
- First Three Badge Times per UserPHONE SCREEN · Seen Apr 2022
- Time of Maximum Mall OccupancyPHONE SCREEN · Seen Apr 2022
- Longest Common Contiguous User ActionsPHONE SCREEN · Seen Apr 2022
- Count a Person’s Distinct FriendsPHONE SCREEN · Seen Mar 2022
- Word Path Moving Only Right or DownPHONE SCREEN · Seen Mar 2022
- UCB1 Multi-Armed Bandit TraceONSITE INTERVIEW · Seen Sep 2026
- Moving Cost by Volume and CategoryONSITE INTERVIEW · Seen Sep 2026
- DFS Subsequences of a String (For MLE)OA · Seen Sep 2024
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Worst-Performing Ads by CTR in a Campaign TreeUber · PHONE SCREENUber interview questions
- Merge N-Ary Trees by KeyLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Diff Two Filesystem Hash TreesCursor · PHONE SCREENCursor interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Streaming Step Progress HierarchyThe Allen Institute for AI · PHONE SCREEN
- Binary Tree Vertical Order TraversalBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Build a Trace ForestDatadog · PHONE SCREENDatadog interview questions
- Top-Down Employee Reporting ForestOkta · ONSITE INTERVIEWOkta interview questions
- Vertical Order Traversal of a Binary TreeAmazon · ONSITE INTERVIEW