Javatpoint Logo
Javatpoint Logo

Find number of Employees Under every Manager

An employee and his boss are mapped in a dictionary as a number of (employee, manager) pairs, as shown below.

Create a function to return the total number of employees, not just their direct reports, that each manager in the hierarchy has. One boss may be presumed to be the only one to whom an employee directly reports. The root node/ceo is listed as reporting to himself in the aforementioned vocabulary.

An output dictionary with the following words should be produced.

There may be more ways to answer this, but I found it fascinating and thought I'd share:

Utilizing memory (Dynamic programming) to determine the number of employees under a management in step 2.b saves us from having to repeat the process for for individual employee. In the code below, the recursive function populateResultUtil() makes use of memoization to prevent re-computation of the same results.

Code in C++

Output:

result = {A=0, B=0, C=2, D=0, E=1, F=5}






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA