Quick revision
The short answer
I would compare the adjacency list, materialized path, nested sets and closure table on read speed, move cost and complexity. For 300,000 employees, an adjacency list with an indexed manager_id and recursive CTEs is usually fast enough for both queries and keeps moves cheap; a closure table makes both queries one indexed lookup but a move rewrites rows in proportion to subtree size times depth. Either way, reorganisations are applied in one transaction or as effective-dated changes, and effective dating (valid_from, valid_to) is needed anyway for historic reporting lines.
What to remember
In this answer, we compared four tree models for an org chart of 300,000 people. A closure table answers both questions with one indexed lookup but makes each moved subtree cost roughly subtree size times depth in row changes, while an adjacency list with an indexed manager_id serves both recursive queries fast enough and keeps moves to one update per subtree root. Reorganisations are applied in one validated transaction, and effective-dated reporting lines give historic reporting and planned changes.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would compare the adjacency list, materialized path, nested sets and closure table on read speed, move cost and complexity. For 300,000 employees, an adjacency list with an indexed manager_id and recursive CTEs is usually fast enough for both queries and keeps moves cheap; a closure table makes both queries one indexed lookup but a move rewrites rows in proportion to subtree size times depth. Either way, reorganisations are applied in one transaction or as effective-dated changes, and effective dating (valid_from, valid_to) is needed anyway for historic reporting lines.
Why this question is asked
Org charts combine the two things trees are bad at: fast queries in both directions, and large structural changes. HR also asks questions about the past, such as who reported to whom when a decision was made. Interviewers ask this design question to see whether we compare models with numbers instead of picking a favourite, and whether we notice the time dimension. In this answer, we will recap the four tree models, estimate their costs for this workload, decide, design reorganisations as consistent changes, and add effective dating.
1A Quick Recap of the Four Tree Models
An adjacency list stores each employee with manager_id pointing to their manager. A materialized path stores the whole chain as a string or array in each row, such as 1.40.512.9001. Nested sets give each node a left and right number so that a subtree is every node whose numbers fall inside its parent's range. A closure table stores one row for every (ancestor, descendant) pair, with the distance between them.
The two queries we must serve are 'all reports under this manager', which reads a subtree downwards, and 'management chain of this person', which reads the path upwards to the CEO.
2Estimating the Costs for 300,000 Employees
Org charts are shallow and bushy: even large companies rarely go beyond 10 to 12 levels. Let us assume an average depth of 7 and see what each model costs.
| Model | Reports under manager | Management chain | Move 2,000 people |
|---|---|---|---|
| Adjacency list | Recursive CTE, one index probe per node | Up to 12 index probes | One update per moved subtree root |
| Materialized path | Indexed prefix search | Parse the path | Rewrite path of every node moved |
| Nested sets | One range query | One range query | Renumber a large part of the tree |
| Closure table | One indexed lookup | One indexed lookup | Delete and insert about subtree x depth rows |
The closure table has about 300,000 x 7 = 2.1 million rows (each employee has around 7 ancestors), which is small for PostgreSQL. Both queries become a single index lookup on ancestor_id or descendant_id. But moving a subtree of 2,000 people under a different branch means deleting every row linking those 2,000 people to their old ancestors above the subtree root and inserting rows to their new ancestors, roughly 2,000 x 7 = 14,000 deletes and a similar number of inserts per subtree. That is fine once, but heavy when a reorganisation moves many subtrees.
Nested sets make reads fastest but renumbering on every move makes them unsuitable for an org chart that changes daily. The materialized path is a reasonable middle ground, but a move still rewrites every moved row.
3Why the Adjacency List Is Often Enough
With an index on manager_id, the recursive query for a whole department reads one index range per manager in the subtree. For a VP with 20,000 people under them, that is 20,000 rows found through index lookups, typically tens of milliseconds. The management chain is at most about 12 primary key lookups, well under a millisecond.
And moves stay trivial: moving a team of 2,000 is updating one manager_id, the team lead's, because everyone else still points to their own manager. So my default choice would be the adjacency list, measured against real queries. If measurements show subtree reads are too slow under load, for example a permissions check that runs 'is X under Y' thousands of times per second, I would add a closure table as a derived structure, rebuilt from the adjacency list, rather than make it the source of truth.
4Applying Reorganisations Consistently
A reorganisation moving thousands of people must not be visible half-done, or a query in the middle would see some teams under the old VP and some under the new one, and permission checks could briefly grant or deny the wrong access. The simplest way is to apply the whole reorganisation in one transaction: other sessions see either the old tree or the new one.
With an adjacency list, the transaction is small, because only subtree roots change. With a closure table, the same reorganisation touches tens of thousands of rows and holds locks longer, which is another point in favour of keeping the closure table derived. Validation inside the transaction (no cycles, no orphan managers) means a bad import fails as a whole.
5Effective Dating for History and Planned Changes
HR almost always needs history: who was this person's manager on 1 March, for a performance review or an audit? And reorganisations are planned in advance with an effective date. Both point to effective dating: instead of overwriting manager_id, we store reporting lines with valid_from and valid_to.
- Load new reporting linesvalid_from = 2026-12-01
- Validate in one transactionNo cycles, no overlaps, managers exist
- Close old linesvalid_to = 2026-12-01
- Date reaches 1 DecemberQueries for today see the new tree
- Rebuild derived closure table?YesRefresh from current linesNoNothing more to do
5 steps in order. Play them, or step through them one at a time.
A reorganisation becomes a set of rows that close the old lines on the effective date and open new ones, which can be loaded days in advance and take effect automatically. The exclusion constraint (with the btree_gist extension) stops an employee from having two managers at the same time. Queries for the current tree filter on today's date first, so the recursive walk runs over 300,000 current rows just as before.
6Follow-up questions
When would you make the closure table the source of truth?
When reads vastly outnumber moves and the read queries need more than simple subtree and chain lookups, for example frequent 'is X anywhere under Y' checks across several hierarchies. Even then, I prefer to generate it from the adjacency list in the same transaction or by a job, so there is one simple place to edit the tree.
How do you handle people with two managers, such as a matrix organisation?
Then reporting lines are no longer a tree but a graph, so we add a relationship type (line manager, project lead) and keep one tree per type. Queries choose which tree they walk, and the single line-manager tree remains the one used for approvals and payroll.
How would you test the performance claim?
Load a realistic synthetic org of 300,000 people with real depth and fan-out, run both queries for the largest managers with EXPLAIN ANALYZE, and simulate the biggest past reorganisation. Numbers from that test settle the choice better than any general rule.
7Conclusion
In this answer, we compared four tree models for an org chart of 300,000 people. A closure table answers both questions with one indexed lookup but makes each moved subtree cost roughly subtree size times depth in row changes, while an adjacency list with an indexed manager_id serves both recursive queries fast enough and keeps moves to one update per subtree root. Reorganisations are applied in one validated transaction, and effective-dated reporting lines give historic reporting and planned changes.
Open with
I would lay out the four options against the two queries and the reorganisation cost. An adjacency list with manager_id is simplest; reports under a manager and the management chain are recursive CTEs.
Then, if they want more
Org charts are shallow, maybe 7 to 12 levels, so the chain is about a dozen index lookups and even a VP with 20,000 reports is tens of milliseconds with manager_id indexed. Moves are cheap, because moving a team of 2,000 only changes the team lead's manager_id.
A closure table, ancestor, descendant and depth, makes both queries a single indexed lookup and has about 2 million rows here, but moving a subtree rewrites roughly subtree size times depth rows, so a reorg moving thousands of people means tens of thousands of row changes.
Nested sets read fastest but renumbering makes them a poor fit for a tree that changes often. So my default is the adjacency list, verified with a realistic load test, and if subtree checks become a hot path I would add a closure table derived from it rather than make it the source of truth.
Reorgs must be consistent, so I apply them in one transaction with validation for cycles and missing managers. And I would model reporting lines with valid_from and valid_to anyway, since HR needs historic reporting lines and planned reorganisations; an exclusion constraint stops overlapping managers, and current queries filter on today's date first.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "An organisation chart service must answer 'all reports under this manager' and 'the...": https://scrivio-interview-library.netlify.app/databases/data-modeling/5791ad3399/