Hone logo
Hone
Problems

The Number of Employees Which Report to Each Employee

Imagine a company with a hierarchical structure where employees report to managers. This challenge asks you to determine, for each employee, how many direct subordinates they have. This information is crucial for understanding organizational structure, resource allocation, and identifying key personnel.

Problem Description

Given a list of employee-manager relationships, you need to calculate the number of employees that directly report to each manager. Each employee, except for the top-level CEO (who has no manager), reports to exactly one manager.

What needs to be achieved: You must output a mapping where each manager's ID is associated with the count of employees who directly report to them.

Key requirements:

  • The output should include all employees who have at least one direct report. Employees who are not managers themselves (i.e., they have no one reporting to them) should not appear in the output.
  • The CEO or top-level employee (who has no manager) should not be included in the output unless they also manage other employees.

Expected behavior: For each employee ID that appears as a manager in the input, the output should show that employee ID and the total count of employees whose managerId matches that employee ID.

Important edge cases to consider:

  • A company with only one employee (the CEO). In this case, no one reports to them, so the output should be empty.
  • A flat organizational structure where several employees report directly to the CEO.
  • A deep organizational structure with multiple levels of management.

Examples

Example 1:

Input:
Employees = [
  {"employeeId": 1, "name": "Alice", "managerId": null},  // CEO
  {"employeeId": 2, "name": "Bob", "managerId": 1},
  {"employeeId": 3, "name": "Charlie", "managerId": 1},
  {"employeeId": 4, "name": "David", "managerId": 2}
]

Output:

{
  "1": 2,  // Alice manages Bob and Charlie
  "2": 1   // Bob manages David
}

Explanation: Employee with employeeId 1 (Alice) is the manager for employees with employeeId 2 (Bob) and 3 (Charlie). Employee with employeeId 2 (Bob) is the manager for employee with employeeId 4 (David). Employee 3 (Charlie) and 4 (David) are not managers.

Example 2:

Input:
Employees = [
  {"employeeId": 10, "name": "Eve", "managerId": null}, // CEO
  {"employeeId": 20, "name": "Frank", "managerId": 10},
  {"employeeId": 30, "name": "Grace", "managerId": 10},
  {"employeeId": 40, "name": "Heidi", "managerId": 10},
  {"employeeId": 50, "name": "Ivan", "managerId": 20}
]

Output:

{
  "10": 3, // Eve manages Frank, Grace, and Heidi
  "20": 1  // Frank manages Ivan
}

Explanation: Employee 10 (Eve) manages 20, 30, and 40. Employee 20 (Frank) manages 50. Employees 30, 40, and 50 are not managers.

Example 3: (Edge case: single employee)

Input:
Employees = [
  {"employeeId": 100, "name": "CEO", "managerId": null}
]

Output:

{}

Explanation: The only employee is the CEO and has no one reporting to them.

Constraints

  • The number of employees will be between 1 and 1000, inclusive.
  • employeeId will be unique integers.
  • managerId will either be an integer representing a valid employeeId or null for the CEO.
  • Each employee will have at most one managerId.
  • There will be exactly one employee with managerId as null (the CEO).
  • The input will be a list of employee objects/dictionaries. The output should be a map/dictionary where keys are manager IDs (as strings or integers, depending on language conventions) and values are integer counts.

Notes

  • You'll need to iterate through the employee list and count occurrences of each managerId.
  • Consider how you will store and update the counts efficiently. A hash map or dictionary is a good candidate data structure.
  • Remember to handle the null managerId appropriately – it doesn't represent a manager to count subordinates for.
Loading editor...
plaintext