KatInterview

Org Tree Reporting Hierarchy Count Service

System DesignModel answer
MediumModel answerSystem Designtreescachingdistributed-systems

Last updated

Design a system to efficiently query total subordinate counts in a corporate hierarchy while handling organizational structure changes.

Design a service for a corporate organizational hierarchy where employees report to managers in a tree structure (each employee has at most one direct manager, executives at the top have none).

Your system must support:

  1. Query operation (high volume): Given an employee ID, return the total number of people in their complete reporting chain (all direct reports, their reports, and so on recursively).

  2. Update operation: Handle changes when an employee moves to a different manager. All subsequent queries must reflect the updated structure.

Consider:

  • How would you model and store this data?
  • What algorithms would you use for queries and updates?
  • How would you maintain query performance under frequent reads with occasional writes?
  • How would you prevent invalid states like reporting cycles?
  • What are the trade-offs in your design?

Assume a large enterprise with hundreds of thousands of employees, query rates in the tens of thousands per second, and organizational changes happening throughout each day.

Hints

Hint 1

The reporting hierarchy forms a forest (collection of trees) - what properties of trees can you exploit?

Hint 2

Consider materializing aggregate data rather than computing it on every query

Hint 3

Think about what changes when one person moves - does the entire tree need recalculation?

Hint 4

Reporting hierarchies in real companies are quite shallow, typically 10-20 levels maximum