Lowest Common Manager for Employee Groups
Problem statement
A company reporting hierarchy is a rooted binary tree. The tree is encoded by parallel arrays employeeIds and managerIds. For each index i, managerIds[i] is the direct manager of employeeIds[i]; the root has manager -1.
You also receive a batch of employee groups in queries. For each group, return the employee ID of its lowest common manager: the deepest employee whose subtree contains every employee in that group. An employee is in their own subtree, so a one-employee query returns that employee.
Examples
Example 1
employeeIds = [1,2,3,4,5,6,7]managerIds = [-1,1,1,2,2,3,3]queries = [[4,5],[4,6,7],[2,4,5]]return = [2,1,2]Employees 4 and 5 report under 2. The group containing 4, 6, and 7 spans both children of root 1. Employee 2 is itself an ancestor of the final group.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 1 more worked example, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.