Question: Given an employee hierarchy represented by managers and reportees, where the CEO is the root, I needed to find the maximum reporting layer (tree height) and the minimum number of reportees to move to directly report to the CEO if the reporting layer exceeds a given height, h.
Details
The coding question I encountered was as follows:
Input:
n (1-n employees)
manager int[]
reportee int[]
node(1) = CEO
reportee[i] reports to manager[i] forming a tree structure: CEO (root node) - reportee
Tasks:
Find the maximum reporting layer from the CEO to the furthest employee (height of the tree).
Determine the minimum number of reportees that need to be moved to directly report to the CEO when the reporting layer is larger than h.
My approach:
Initially, I planned to construct the tree structure using the given manager and reportee arrays.
Then, I was going to calculate the height of the tree using a depth-first search (DFS) approach.
Finally, if the height exceeded 'h', I was going to determine the minimum number of reportees to move to directly report to the CEO, which I found challenging within the given timeframe.