471. Closest Common Parent Group for Employees
Closest Common Parent Group for Employees

Implement an employee directory that returns the deepest group containing every employee in a query. A group contains its direct employees and all employees in its descendant groups. The deepest group is the one farthest from the root.

Group Hierarchy

  • The hierarchy is a valid, connected tree with one root group. Every other group has exactly one parent.
  • A group may have any number of subgroups and may also contain employees directly.
  • Each employee belongs directly to exactly one group.

Methods

EmployeeDirectory(String rootGroupId, List<String> groupRelations, List<String> employeeMemberships)
  • rootGroupId: The root group's ID.
  • groupRelations: Each string is "parentGroupId,childGroupId". Group IDs come from these relations and rootGroupId.
  • employeeMemberships: Each string is "employeeId,groupId", assigning an employee to an existing group.
String getCommonGroupForEmployees(List<String> employeeIds)
  • Return the ID of the deepest group containing every requested employee. The root is a valid answer.
  • For one employee, return their direct group.
  • Ignore repeated employee IDs.
  • Return "" if the query is empty or any requested employee does not exist.

Constraints

  • Number of groups: 1 to 100,000. The number of group relations is one less than the number of groups.
  • Number of employees: 1 to 100,000.
  • Number of employee IDs per query: 0 to 100,000.
  • IDs are nonempty, case-sensitive strings without commas. Group IDs are unique, and employee IDs are unique.
  • The hierarchy may be deeply nested, including a chain of groups.

Optional Follow-Ups

Discuss these independent extensions to the tree version. The empty-query, unknown-employee, and duplicate-ID rules still apply.

  • Shared groups and memberships: Allow multiple parents per group and multiple groups per employee, with an acyclic hierarchy. Return a common group with no proper descendant that also contains every requested employee. If several qualify, return the lexicographically smallest group ID using String.compareTo. Return "" if no common group exists.
  • Concurrent updates: Discuss four hierarchy update operations; their signatures are unspecified. Each query must read one complete, consistent state incorporating updates completed before the query begins. A query overlapping an update may see the state before or after that update, but never a partially applied update.
  • Flat groups: Remove subgroups and allow employees to belong to multiple groups. Return the lexicographically smallest group ID using String.compareTo among groups containing every requested employee, or "" if none qualify.

Examples

Named arguments and bracketed lists below are pseudocode. All queries use this directory:

EmployeeDirectory(
  rootGroupId = "org",
  groupRelations = ["org,technology", "org,finance", "technology,services", "technology,apps", "services,storage", "finance,payroll"],
  employeeMemberships = ["e1,storage", "e2,storage", "e3,services", "e4,apps", "e5,payroll", "e6,org"]
)

  • getCommonGroupForEmployees(employeeIds = ["e1", "e2"])
    Output: "storage". Both employees belong directly to storage.
  • getCommonGroupForEmployees(employeeIds = ["e1", "e3"])
    Output: "services". Services directly contains e3 and contains e1 through storage.
  • getCommonGroupForEmployees(employeeIds = ["e1", "e3", "e4"])
    Output: "technology". Technology contains employees in both services and apps.
  • getCommonGroupForEmployees(employeeIds = ["e2", "e5"])
    Output: "org". The employees are in different branches of the root.
  • getCommonGroupForEmployees(employeeIds = ["e1", "e6"])
    Output: "org". Employee e6 belongs directly to the root.
  • getCommonGroupForEmployees(employeeIds = ["e4"])
    Output: "apps". A single employee's direct group is returned.
  • getCommonGroupForEmployees(employeeIds = ["e1", "e1"])
    Output: "storage". Repeated IDs are ignored.
  • getCommonGroupForEmployees(employeeIds = [])
    Output: "". The query is empty.
  • getCommonGroupForEmployees(employeeIds = ["e1", "missing"])
    Output: "". Employee missing does not exist.


Please use Laptop/Desktop or any other large screen to add/edit code.