KatInterview

Org Chart Inference with Manager and Peer Links

Data Structure DesignModel answer
MediumModel answerData Structure Designunion-finddesigngraphs

Last updated

Design a structure that records manager and same-manager links between employee IDs and answers whether one employee sits above another, including facts inferred from earlier updates.

Design an in-memory class that tracks reporting relationships among employees identified by ID. It must support three operations:

  1. add_manager(a, b): record that a is the direct manager of b.
  2. add_peer(a, b): record that a and b report to the same direct manager. That manager may not be known yet.
  3. is_manager(a, b): return true if a is a manager of b, and false otherwise.

Answers must reflect everything that can be inferred from earlier calls. For example, after add_manager("m", "x") and add_peer("x", "y"), the call is_manager("m", "y") must return true, because y shares x's manager.

Assumptions:

  • The recorded facts are logically consistent, and the org chart never contains a cycle.
  • Calls can arrive in any order. A peer fact may be recorded before anyone knows who the manager is.
  • Treat "manager of" as meaning any level above: if m manages x and x manages y, then is_manager(m, y) is true. Mention how your design changes if only direct managers should count.

Describe your data structures, implement the three operations, and give time and space complexity. Also discuss the edge cases you would handle.

Hints

Hint 1

Ask which relation is an equivalence relation. 'Has the same direct manager as' is reflexive, symmetric and transitive, so it splits employees into disjoint groups that merge over time.

Hint 2

Store the manager once per peer group rather than once per person. If the group's representative holds the manager ID, every present and future member of the group gets it automatically.

Hint 3

When two groups merge, keep whichever manager is known. To answer a query, repeatedly jump from the person to their group's manager and compare with the candidate, stopping when no manager is known.