Design org chart lookup with preprocessing
PatreonSoftware Engineer · Technical ScreenLast updated
This question evaluates skills in data structures and algorithms, specifically tree/forest representation, aggregation of subtree metrics, dynamic updates, and rigorous time and space complexity analysis.
You are given an organization chart represented as a list of pairs [managerName, directReportName]. Names are unique strings. The data may form a forest (multiple top-level managers) but contains no cycles.
Implement an in-memory data structure that supports:
lookUp(name)→ returns a tuple:
role: either"mgr"if the person has at least one direct report, otherwise"ic"directReportCount: number of direct reports (out-degree)totalReportCount: number of total reports in their subtree (all descendants)
-
Analyze the time and space complexity of your approach.
-
If there are a large number of
lookUprequests, how would you optimize the system? -
If
lookUpmust be O(1) time, what preprocessing would you do, and what would be the preprocessing cost?
Follow-up:
- Implement
add(managerName, reportName)that adds a new reporting relationship (and creates missing nodes if needed). After adds,lookUpshould reflect the updated org chart. Discuss/update complexity under your design.
You should be able to run your own test cases to validate correctness.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.