Compute org height and minimal CEO promotions
Company: Rippling
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
You are given an organization chart with **n** employees labeled `1..n`. Employee `1` is the CEO (root).
The reporting relationships are given as two integer arrays `manager[]` and `reportee[]` of length `n-1`, where for each index `i`:
- `reportee[i]` reports directly to `manager[i]`
Assume these pairs form a valid **tree** rooted at the CEO.
Define an employee’s **reporting layer** (level) as their distance from the CEO in nodes, with:
- `level(1) = 1` (CEO)
- each direct report of the CEO has level `2`, etc.
### Task
1. Compute the **maximum reporting layer** in the company (i.e., the tree height in levels): the maximum `level(v)` over all employees `v`.
2. Given an integer threshold `h`, deep reporting chains beyond `h` layers are considered harmful. You may perform the following operation any number of times:
- Choose any employee `x != 1` and change their manager so that `x` now **reports directly to the CEO** (i.e., make `manager(x) = 1`). The entire subtree of `x` moves with them.
Return the **minimum number of such moves** required so that in the resulting org chart, the maximum reporting layer is **≤ h**.
### Input/Output requirements
- **Input:** `n`, arrays `manager[]`, `reportee[]`, and integer `h`.
- **Output:**
- The original maximum reporting layer (height).
- The minimum number of moves to make the final height ≤ `h`.
### Notes / Assumptions
- The given relationships form a connected rooted tree with root `1`.
- Clarify in your solution whether you treat “height” as number of nodes on the longest root-to-leaf path (levels, as defined above) rather than number of edges.
Quick Answer: This question evaluates understanding of rooted tree data structures, computation of tree height (reporting layers), and algorithmic manipulation of subtrees to minimize maximum depth, testing skills in tree traversal, depth calculation, and constrained optimization.
Part 1: Maximum Reporting Layer in an Org Tree
You are given an organization chart with employees labeled 1..n, where employee 1 is the CEO. The arrays manager and reportee each have length n-1, and reportee[i] reports directly to manager[i]. These relationships form a valid rooted tree. Define reporting layer as the number of nodes on the path from the CEO: level(1) = 1, direct reports of the CEO are at level 2, and so on. Compute the maximum reporting layer in the company. In this problem, height is measured in levels (nodes), not edges.
Constraints
- 1 <= n <= 100000
- len(manager) == len(reportee) == n - 1
- Employees are labeled from 1 to n
- Employee 1 is the root (CEO)