Company: Rippling
Difficulty: medium
A company stores its reporting structure as an array managers of length n , where employees are numbered from 1 to n . managers[i] is the id of the direct manager of employee i + 1 (the array is 0-indexed, the employee ids are 1-indexed). The CEO has no manager and is marked with -1 . The structure is always a valid tree. The level of an employee is the number of manager links between that employee and the CEO; the CEO sits at level 0 . The height of the org tree is the largest level in the company. For organizational efficiency the CEO wants the height of the org tree to be at most h . The only change that is allowed is a reassignment : pick an employee and make that employee report directly to the CEO . When an employee is reassigned, the whole subtree under that employee moves with the employee — every one of their subordinates keeps the same direct manager as before. Each reassignment adds one more direct report to the CEO, so the CEO wants to perform as few of them as possib