Maximum Average N-ary Subtree
Updated
Problem statement
You are given a tree in which each node may have any number of children. Its nodes are 0 through n - 1. Node 0 is the root, parent[0] = -1, and parent[i] is the parent of every other node. Node i has integer value values[i].
The subtree rooted at a node contains that node and all its descendants. Only consider subtrees whose root has at least one child. Return the root index of the subtree with the largest average node value. Compare averages exactly. If several subtrees tie, return the smallest root index. Return -1 if no root has a child.
Function
maximumAverageSubtree(parent: int[], values: int[]) → intExamples
Example 1
parent = [-1,0,0,1,1,1,2,2]values = [20,12,18,11,2,3,15,8]return = 2The averages at candidate roots 0, 1, and 2 are 89/8, 28/4, and 41/3. Node 2 is best.
Example 2
parent = [-1,0,0,1]values = [0,0,0,0]return = 0Both candidate roots 0 and 1 have average zero. The smaller index wins.
Constraints
1 <= n == parent.length == values.length <= 20000.- The parent array describes one valid tree rooted at 0; a parent index need not be smaller than its child index.
-10000 <= values[i] <= 10000.
More Rippling Phone Screen questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dependency-Aware Task SchedulerPHONE SCREEN · Seen Nov 2026
- Delivery Cost Tracker with Effective-Dated RatesPHONE SCREEN · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best and Worst Camel Card CompletionsPHONE SCREEN · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- In-Memory Priority Task SchedulerPHONE SCREEN · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Median Across N Sorted ArraysPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- House Robber IIIOkta · PHONE SCREENOkta interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Validate Binary Search TreeKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
- House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- House Robber IIIVisa · PHONE SCREENVisa interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions