Limit an Organization Tree's Height
Learn this problemProblem statement
An organization has n employees numbered from 1 to n. Employee 1 is the CEO. Each pair managers[i], reportees[i] is a directed edge from a manager to a direct report. The edges form a rooted tree.
An employee's level is its number of edges from the CEO, so the CEO is at level 0. The tree's height is its maximum employee level.
Restructuring Rule
The organization wants height at most maxHeight. In one change, choose any non-CEO employee and make that employee report directly to the CEO. The employee's entire subtree moves with them and otherwise remains unchanged.
Return [originalHeight, minimumChanges], where minimumChanges is the fewest direct-report changes needed to make the final height at most maxHeight.
Interviewer follow-ups
The interview begins by asking for the deepest employee level. The follow-up adds the maximum-height policy and asks candidates to minimize how many employees must become direct reports of the CEO.
Function
analyzeOrgTree(n: int, managers: int[], reportees: int[], maxHeight: int) → int[]Examples
Example 1
n = 6managers = [1,2,3,4,1]reportees = [2,3,4,5,6]maxHeight = 2return = [4,1]The chain 1-2-3-4-5 gives an original height of 4. Making employee 3 report to the CEO changes that chain to 1-3-4-5, whose deepest level is 3, so it does not satisfy maxHeight = 2. Instead, make employee 4 report to the CEO, leaving employee 5 at level 2. One change is sufficient.
Example 2
n = 5managers = [1,1,2,2]reportees = [2,3,4,5]maxHeight = 2return = [2,0]The original tree already has height 2, so no direct-report change is needed.
Constraints
2 <= n <= 200000managers.length = reportees.length = n - 11 <= maxHeight < n- The edges form a tree rooted at employee 1.
More Rippling problems
- Delivery Cost TrackerPHONE SCREEN · Seen Jul 2026
- Corporate Card Expense RulesPHONE SCREEN · Seen Jun 2026
- Camel CardsPHONE SCREEN · Seen May 2026
- Article Vote TrackerPHONE SCREEN · Seen May 2026
- Employee Resource Access ManagementONSITE INTERVIEW · Seen Jan 2026
- Distributed System RecoveryOA · Seen Jul 2025
- Server Upgrade PlanningOA · Seen Jul 2025
- Shortest SubstringOA · Seen Jul 2025