KatInterview

Employee Hierarchy: Counting Trees, Level Traversal, and Ancestor Search

Trees & GraphsModel answer
MediumModel answerTrees & Graphstree-constructionlevel-order-traversallowest-common-ancestor

Last updated

Build a management structure from employee records, then count independent hierarchies, display staff by organizational depth, and locate shared supervisors.

You receive employee data as a collection of (id, title, reports_to) records. The reports_to field contains the employee ID of that person's direct supervisor, or None for top-level executives who report to no one. Your task is to implement three functions operating on this data structure.

Consider this dataset throughout:

employees = [
 (4, "Engineer", 2),
 (1, "CEO", None),
 (8, "Designer", 7),
 (2, "VP Engineering", 1),
 (6, "Account Executive", 3),
 (3, "VP Sales", 1),
 (5, "Engineer", 2),
 (7, "Founder", None),
]

Assumptions

  • Each ID appears exactly once in the input
  • Job titles may repeat and serve only as labels
  • Records appear in arbitrary order—a supervisor may be listed after their direct reports
  • You may assume the data is well-formed: all non-None reports_to values reference valid employee IDs, and no circular reporting relationships exist

Challenge 1: Count Independent Hierarchies

Determine how many separate organizational hierarchies exist. A hierarchy consists of one top-level executive (with reports_to = None) plus everyone who reports through that chain of command. The example data contains 2 hierarchies.

Your implementation should:

  • Run in O(n) time where n is the number of employees
  • Handle an empty employee list correctly
  • Make no assumptions about input ordering

Challenge 2: Display by Organizational Depth

Output all employees grouped by their distance from a top-level executive. Top executives appear on the first line, their direct reports on the second, and so on. For the example, valid output includes:

1 (CEO), 7 (Founder)
2 (VP Engineering), 3 (VP Sales), 8 (Designer)
4 (Engineer), 5 (Engineer), 6 (Account Executive)

Requirements:

  • Employees at the same depth across different hierarchies may share a line
  • Within each line, use a consistent ordering (by ID, by first appearance, or grouped by supervisor—your choice, but be explicit)
  • Achieve O(n) time and space complexity

Challenge 3: Locate Shared Supervisor

Given two employee IDs, find the most immediate person both report to. This is the first employee who appears in both upward reporting chains. Examples:

  • Employees 4 and 5 → 2
  • Employees 4 and 6 → 1
  • Employees 4 and 8 → no shared supervisor (different hierarchies)

Clarifications needed:

  • If one employee is the direct or indirect supervisor of the other, should the answer be that employee or their own manager?
  • What value indicates "no shared supervisor" for employees in different hierarchies?
  • How should invalid employee IDs be handled?

Your solution should:

  • Process each query in O(h) time where h is the hierarchy depth
  • Handle edge cases: same employee ID twice, non-existent IDs, employees in different trees

Hints

Hint 1

Build a dictionary mapping each employee ID to their manager ID, and another mapping each manager to their list of direct reports—reuse these for all three parts.

Hint 2

For counting hierarchies, identify all employees with reports_to = None; each represents one independent tree.

Hint 3

For level-order output, use breadth-first search starting from all top-level executives simultaneously, or process each hierarchy separately.

Hint 4

To find a shared supervisor, collect all ancestors of one employee into a set, then walk up from the second employee until you find a match.