Lowest Common Manager of K Employees in an N-ary Org Tree

Quick Overview

Given an org chart encoded as manager rows, find the deepest employee who manages every id in a target list, returning -1 for an empty chart or a missing id. Tests building a tree from an adjacency list, iterative post-order traversal and counting targets per subtree.

Lowest Common Manager of K Employees in an N-ary Org Tree

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an N-ary tree representing a company org chart. The tree is described by a list of rows: the first row contains a single value, the `employeeId` of the root, and every subsequent row has the form `[managerId, childId1, childId2, ...]` listing the employees that `managerId` directly manages. Every `employeeId` is unique. Given a list `targets` of K employee ids, return the `employeeId` of their **lowest common manager**: the deepest node in the tree that is an ancestor (direct or indirect manager) of all K target employees. A node is considered an ancestor of itself. Return `-1` if the tree is empty, if `targets` contains fewer than two ids, or if any target id does not appear in the tree. ### Function Signature ```python def lowest_common_manager(tree: list[list[int]], targets: list[int]) -> int: ``` ### Rules - An empty org chart is passed as `tree = []`. Otherwise `tree[0]` is `[rootId]` and the remaining rows describe one valid tree rooted there, so every employee other than the root has exactly one manager. - An employee with no direct reports does not need a row of their own (employees 5 to 9 in the examples have none). - The fewer-than-two check uses the length of `targets` as given. After that check, a repeated id counts once, so `targets = [5, 5]` on the example chart returns `5`. - If one target manages another, the managing target can be the answer, because a node is its own ancestor. ### Constraints - `0 <= number of nodes <= 10^4` - `tree[0]` holds exactly one value, the root id, when the tree is non-empty - Each row after the first has the form `[managerId, childId1, ...]` - All `employeeId` values are unique and in the range `[1, 10^6]` - `2 <= len(targets) <= 100` ### Examples All three examples use this chart: ```text tree = [[1], [1, 2, 3, 4], [2, 5, 6], [3, 7], [4, 8, 9]] 1 / | \ 2 3 4 / \ | / \ 5 6 7 8 9 ``` ```text Input: tree = [[1], [1, 2, 3, 4], [2, 5, 6], [3, 7], [4, 8, 9]], targets = [5, 6] Output: 2 ``` Both 5 and 6 are directly managed by 2, and neither of them manages the other, so the deepest common manager is 2. ```text Input: tree = [[1], [1, 2, 3, 4], [2, 5, 6], [3, 7], [4, 8, 9]], targets = [5, 7, 9] Output: 1 ``` 5 is under 2, 7 is under 3, and 9 is under 4, so only the root 1 manages all three. ```text Input: tree = [[1], [1, 2, 3, 4], [2, 5, 6], [3, 7], [4, 8, 9]], targets = [4, 9] Output: 4 ``` 9 reports directly to 4, and 4 counts as its own ancestor, so 4 is the deepest node whose subtree holds both targets.

Overview: Given an org chart encoded as manager rows, find the deepest employee who manages every id in a target list, returning -1 for an empty chart or a missing id. Tests building a tree from an adjacency list, iterative post-order traversal and counting targets per subtree.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an N-ary tree representing a company org chart. The tree is described by a list of rows: the first row contains a single value, the employeeId of the root, and every subsequent row has the form [managerId, childId1, childId2, ...] listing the employees that managerId directly manages. Every employeeId is unique.

Given a list targets of K employee ids, return the employeeId of their lowest common manager: the deepest node in the tree that is an ancestor (direct or indirect manager) of all K target employees. A node is considered an ancestor of itself.

Return -1 if the tree is empty, if targets contains fewer than two ids, or if any target id does not appear in the tree.

Function Signature

def lowest_common_manager(tree: list[list[int]], targets: list[int]) -> int:

Rules

  • An empty org chart is passed as tree = [] . Otherwise tree[0] is [rootId] and the remaining rows describe one valid tree rooted there, so every employee other than the root has exactly one manager.
  • An employee with no direct reports does not need a row of their own (employees 5 to 9 in the examples have none).
  • The fewer-than-two check uses the length of targets as given. After that check, a repeated id counts once, so targets = [5, 5] on the example chart returns 5 .
  • If one target manages another, the managing target can be the answer, because a node is its own ancestor.

Constraints

  • 0 <= number of nodes <= 10^4
  • tree[0] holds exactly one value, the root id, when the tree is non-empty
  • Each row after the first has the form [managerId, childId1, ...]
  • All employeeId values are unique and in the range [1, 10^6]
  • 2 <= len(targets) <= 100

Examples

All three examples use this chart:

tree = [[1], [1, 2, 3, 4], [2, 5, 6], [3, 7], [4, 8, 9]]

        1
      / | \
     2  3  4
    / \ |  / \
   5  6 7 8   9
Input: tree = [[1], [1, 2, 3, 4], [2, 5, 6], [3, 7], [4, 8, 9]], targets = [5, 6]
Output: 2

Both 5 and 6 are directly managed by 2, and neither of them manages the other, so the deepest common manager is 2.

Input: tree = [[1], [1, 2, 3, 4], [2, 5, 6], [3, 7], [4, 8, 9]], targets = [5, 7, 9]
Output: 1

5 is under 2, 7 is under 3, and 9 is under 4, so only the root 1 manages all three.

Input: tree = [[1], [1, 2, 3, 4], [2, 5, 6], [3, 7], [4, 8, 9]], targets = [4, 9]
Output: 4

9 reports directly to 4, and 4 counts as its own ancestor, so 4 is the deepest node whose subtree holds both targets.