27. High-Report Managers

A company’s HR system keeps track of employees and their reporting structure. The management wants to identify managers who have at least 5 employees directly reporting to them.

The company maintains an Employee table that stores:

╔═════════════╦══════════╗
β•‘ Column Name β•‘Β  Β TypeΒ  Β β•‘
╠═════════════╬══════════╣
β•‘Β  Β  Β idΒ  Β  Β  β•‘Β  Β intΒ  Β  β•‘
║─────────────┼──────────║
β•‘Β  Β  nameΒ  Β  Β β•‘ varcharΒ  β•‘
║─────────────┼──────────║
β•‘ departmentΒ  β•‘ varcharΒ  β•‘
║─────────────┼──────────║
β•‘Β  managerldΒ  β•‘Β  Β intΒ  Β  β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•
  • id: A unique identifier (primary key) for each employee.
  • name: The name of the employee.
  • department: The department in which the employee works.
  • managerId: The id of the employee’s direct manager (NULL if the employee has no manager).

Your task is to find all managers who have 5 or more employees directly reporting to them.

The result should be returned in any order.

The sample output format is in the following example.

Example 1:

Input:

Employee Table

╔══════════╦══════════╦════════════╦═══════════╗
β•‘Β  Β IdΒ  Β  Β β•‘Β  Β nameΒ  Β β•‘ department β•‘ managerId β•‘
╠══════════╬══════════╬════════════╬═══════════╣
β•‘Β  Β 101Β  Β  β•‘Β   Emily Β β•‘Β  Β  Β AΒ  Β  Β  β•‘Β  Β NullΒ  Β  β•‘
║──────────┼──────────┼────────────┼───────────║
β•‘Β  Β 102Β  Β  β•‘Β  Β JakΒ  Β  β•‘Β  Β  Β AΒ  Β  Β  β•‘Β  Β  101Β  Β  β•‘
║──────────┼──────────┼────────────┼───────────║
β•‘Β  Β 103Β  Β  β•‘Β  OliveΒ  Β β•‘Β  Β  Β AΒ  Β  Β  β•‘Β  Β  101Β  Β  β•‘
║──────────┼──────────┼────────────┼───────────║
β•‘Β  Β 104Β  Β  β•‘Β  Β EmyΒ  Β  β•‘Β  Β  Β AΒ  Β  Β  β•‘Β  Β  101Β  Β  β•‘
║──────────┼──────────┼────────────┼───────────║
β•‘Β  Β 105Β  Β  β•‘Β  Β LiamΒ  Β β•‘Β  Β  Β AΒ  Β  Β  β•‘Β  Β  101Β  Β  β•‘
║──────────┼──────────┼────────────┼───────────║
β•‘Β  Β 106Β  Β  β•‘Β  Β LieΒ  Β  β•‘Β  Β  Β BΒ  Β  Β  β•‘Β  Β  101Β  Β  β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•

Output:

╔══════════╗
β•‘Β  Β nameΒ  Β β•‘
╠══════════╣
β•‘Β  EmilyΒ  Β β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•

Explanation:

  • Emily (id = 101) - Has 5 direct reports (102, 103, 104, 105, 106), so he is included.
  • Jak (id = 102), Olive (id = 103), Emy (id = 104), Liam (id = 105), Lie (id = 106) - None of them have 5 or more reports, so they are excluded.

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Hints

0
Β 
Test Case

Input:

Employee