1225. Busiest Manager

Easy1.5 points estimatedHash TableCounting3 hints

A company stores its org chart as a map reportsTo: each key is an employee id and its value is the id of that employee's direct manager. Nobody reports to themselves, and a manager may or may not appear as a key (the top person has no manager entry).

Return the id of the manager who has the most direct reports. If several managers tie for the most, return the smallest id among them. The map has at least one entry, so an answer always exists.

Count direct reports per manager id with a hash map. The intended solution runs in O(n) time and O(n) space, where n is the number of entries.

Example 1

Input:
reportsTo = {"2":1,"3":1,"4":3,"5":3,"6":3}
Output:
3
Explanation:

Manager 1 has 2 reports and manager 3 has 3, so 3 is the busiest.

Example 2

Input:
reportsTo = {"10":20}
Output:
20
Explanation:

Only one manager exists, id 20.

Example 3

Input:
reportsTo = {"5":9,"6":8,"7":9,"10":8}
Output:
8
Explanation:

Managers 9 and 8 both have 2 reports, so the smaller id 8 wins.

Constraints

1 ≤ reportsTo.size ≤ 1000

1 ≤ ids ≤ 109

No employee reports to itself.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…