Root Job with the Largest Total Duration
Updated
Problem statement
A job file describes one or more trees of jobs. The arrays ids, parentIds and durations have the same length. Entries at the same index describe one job. A parent of -1 represents the source file’s null root parent. Otherwise the parent is another job ID. IDs are unique and the relationships have no cycles.
A job’s total duration is its own duration plus the durations of all descendants. Return the ID of the root with the largest total. Add the duration of every descendant. You are counting all the work, not how much time the jobs would take when run together or along the longest path.
For this exercise, assume input records may arrive in any order, durations are nonnegative integer units, equal totals choose the smaller root ID, and an empty file returns -1. A job has at most fifteen direct children.
Further discussion
Explain how a min-heap of size three can retain the three largest root totals as a self-check. This discussion does not change the required result: return the root with the largest total, not the third largest.
Function
largestRootJob(ids: int[], parentIds: int[], durations: int[]) → intExamples
Example 1
ids = [10,11,12,20]parentIds = [-1,10,10,-1]durations = [2,4,5,10]return = 10Descendant sum beats single long root.
Example 2
ids = [7,2]parentIds = [-1,-1]durations = [5,5]return = 2Tie chooses smaller root identity.
Example 3
ids = []parentIds = []durations = []return = -1Empty job file.
Constraints
0 <= ids.length = parentIds.length = durations.length <= 512.- IDs are distinct positive integers at most
10^9. - Each parent is
-1or another input ID; the input is an acyclic forest. - Every job has at most
15direct children. 0 <= durations[i] <= 10^6; totals fit a signed 32-bit integer.
More College Vine interview questions
Similar questions at other companies
- Merge Event PreferencesAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN
- Lowest Common Ancestor in an N-ary TreeGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Lowest Common Ancestor in an N-ary TreeAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Tree Selection and VisibilityJane Street · PHONE SCREENJane Street interview questions
- Account Roles With Parent-User InheritanceStripe · PHONE SCREENStripe interview questions
- Worst-Performing Ads by CTR in a Campaign TreeUber · PHONE SCREENUber interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions