Count Each Person's Direct Parents
Updated
Problem statement
You receive parent-child relationships. Each row [parent, child] states that the first person is a direct parent of the second person.
Return one row [person, count] for every person mentioned in any relationship. The count is the number of distinct direct parents listed for that person. A person mentioned only as a parent must still appear with count zero. Do not count indirect ancestors.
For this exercise, assume that repeated copies of the same relationship count once. Sort the result by person ID in ascending numeric order. If there are no relationships, return an empty array.
Function
countDirectParents(relationships: int[][]) → int[][]Examples
Example 1
relationships = [[1,3],[2,3]]return = [[1,0],[2,0],[3,2]]People 1 and 2 have no listed parents. Person 3 has the two distinct direct parents 1 and 2.
Example 2
relationships = [[7,9],[7,10],[7,9],[11,10]]return = [[7,0],[9,1],[10,2],[11,0]]The repeated [7,9] pair still gives person 9 only one parent. Person 10 has parents 7 and 11. People 7 and 11 have zero listed parents.
Constraints
- For this exercise, assume
0 <= relationships.length <= 2000. - Every relationship contains exactly two IDs.
- Every ID is an integer from 1 to 10^9, inclusive.
- Parent and child IDs differ, and the relationship graph is acyclic.
- There is no requirement that IDs be consecutive or that a parent ID be smaller than a child ID.
- These are abstract graph relationships. A person may have more than two listed direct parents.
More Intuit Online Assessment questions
- Employee Promotion OpportunitiesOA · Seen Oct 2026
- Next Pascal Row CoefficientOA · Seen Oct 2026
- Parse Router LogsOA · Seen Oct 2026
- Longest Repeated Letter Block with Adjacent SwapsOA · Seen Oct 2026
- Maximum Square-Free DivisorOA · Seen Oct 2026
- Two Tribes from Taller-Person CountsOA · Seen Oct 2026
- Shortest Path from an Encoded GraphOA · Seen Sep 2026
- Oldest and Youngest EmployeesOA · Seen Sep 2026
- Pressure-Isolation Shutdown OrderOA · Seen Sep 2026
- Submask Sum QueriesOA · Seen Sep 2026
Similar questions at other companies
- Nodes With Zero or One ParentCompass International Holdings · OACompass International Holdings interview questions
- Top Mutual-Friend RecommendationsVisa · OAVisa interview questions
- Top Mutual-Friend RecommendationsSuperhuman · OASuperhuman interview questions
- Evolving Merchant Clusters and Persistent PinsStripe · OAStripe interview questions
- 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 RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Top-Down Employee Reporting ForestOkta · ONSITE INTERVIEWOkta interview questions
- Top K Users by Distinct ContactsGoogle · PHONE SCREENGoogle interview questions