Find Root of N-Ary Tree
Updated
Problem statement
An N-ary tree has one node for every distinct integer in values. Each pair [parent, child] in edges is a directed tree edge from a node to one of its children.
The edges form exactly one valid rooted tree over all labels in values. Return the label of its root.
Function
findRoot(values: int[], edges: int[][]) → intExamples
Example 1
values = [1,2,3,4,5,6]edges = [[1,3],[1,2],[1,4],[3,5],[3,6]]return = 1Every label except 1 appears as a child, so 1 is the root.
Example 2
values = [7]edges = []return = 7A one-node tree has no edges, and its only label is the root.
Example 3
values = [5,1,9,4]edges = [[9,5],[9,1],[1,4]]return = 9The child labels are 5, 1, and 4. The remaining label 9 is the root.
Constraints
1 <= values.length <= 100000.- All labels in
valuesare distinct integers between-10^9and10^9. edges.length == values.length - 1.- Every edge contains two labels from
values. - Every non-root label appears exactly once as a child, and the edges form one valid N-ary tree.
More Bloomberg LP Phone Screen questions
- Pascal's TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Valid Triangle NumberPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Flat Records to a Hierarchical TreeAddepar · PHONE SCREEN
- Render a Comment HierarchyAmazon · ONSITE INTERVIEW
- Flat Comments to a Nested TreeByteDance · PHONE SCREENByteDance interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Predicate Expression Tree EvaluatorMongoDB · PHONE SCREENMongoDB interview questions
- Render a Stable Comment HierarchyBobyard · PHONE SCREEN
- Delete Selected Nodes from a Parent-Array ForestSnowflake · PHONE SCREENSnowflake interview questions
- Construct a Tree from Level-Order and Inorder TraversalsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Find Nodes with Invalid Parent LevelsZip · ONSITE INTERVIEWZip interview questions
- Validate BST Node DescriptionsAirbnb · OAAirbnb interview questions
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)