I recently encountered this coding challenge during a screening interview at Google on March 15th, 2024. Recognizing its potential to benefit others, I've decided to share it. Given its complexity and relevance, this problem could be categorised as a LeetCode Easy question, making it an invaluable addition to coding practice resources.

Imagine you're exploring the intricate organizational structure of Google, represented as a graph. In this structure:

Each node N corresponds to an individual.
If N reports to M (i.e., M is N's boss), then M is the parent of N.
If P reports to N (i.e., N is P's boss), then P is the child of N.
At Google, an individual's "employee score" is defined as the total number of people they supervise, including themselves.

Your task is to design a function that computes the employee score for any individual within this organizational structure.
image

In this scenario, Alice's employee score is 4, as she supervises Bob, Charlie, Dave, and herself.

Additional Notes:

The hierarchy may include multiple levels of management.
You may assume that there are no cycles in the hierarchy (e.g., no circular reporting relationships).
Each individual appears only once in the hierarchy.

Follow Up Question:

Suppose the graph has already been precomputed with everyone's employee score. Write a function to recompute these employee scores should someone move teams.

Assume that if an individual moves teams, their reports do not move with them, and simply start directly reporting to an individual's former boss. Assume that a function to perform the actual team movement already exists, and the new children/parent of this node have been updated accordingly.

Comments (37)