Employee Hierarchy
GoogleOnsiteMedium
You manage a company's reporting structure. Every employee has an integer id and at most one manager. The first employee added, with no manager, is the CEO. Employees are ordered by when they joined: the order of successful add_employee calls. An id that was deleted can be added again later, and then joins as a new employee.
Part 1: Add and delete employees
Implement the four methods of EmployeeHierarchy.
add_employee(employee_id, manager_id) adds an employee under the manager manager_id and returns True. It returns False and changes nothing when employee_id is already in the company, when manager_id is not an existing employee, or when manager_id is None and the company already has employees. Passing manager_id = None adds the CEO, which is only allowed while the company is empty.
delete_employee(employee_id) removes the employee and returns True, or returns False when the id is not in the company. When the deleted employee has direct reports, the earliest-joined direct report takes their place: it now reports to the deleted employee's manager, or becomes the CEO if the deleted employee was the CEO. The other direct reports now report to that employee.
manager_of(employee_id) returns the id of the employee's manager, or None for the CEO and for an id not in the company.
reports(employee_id) returns the ids of the employee's direct reports in increasing order, or an empty list for an id not in the company.
- Example 1
- init
[]- operations
[["add_employee",[1,null]],["add_employee",[2,1]],["add_employee",[3,1]],["add_employee",[4,2]],["add_employee",[5,2]],["reports",[1]],["manager_of",[4]],["delete_employee",[2]],["reports",[1]],["reports",[4]],["manager_of",[5]],["delete_employee",[5]],["reports",[4]],["manager_of",[3]]]- Output
[true,true,true,true,true,[2,3],2,true,[3,4],[5],4,true,[],1]
Why: Employees 4 and 5 report to 2. Deleting 2 promotes 4, who joined first: 4 now reports to 1 and 5 reports to 4.
Constraints
1 <= employee_id <= 10^91 <= manager_id <= 10^9ormanager_idisNone- At most
10^4calls in total