← Questions

Everyone under a manager in an org chart

0
NetflixCodingLast asked Apr 2026

A company's org chart is a tree: every employee except the top one has one manager. Given the chart and one manager, walk it depth-first and return everyone in that manager's organization, direct reports and everyone below them.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • reports: dict[str, list[str]] maps each manager to their direct reports, in a fixed order; people with no reports may be missing from it.
  • everyone_under(reports, manager: str) -> list[str] — the manager's whole organization in depth-first order (a report, then that report's organization, then the next report), not including the manager.

Example

reports = {"ana": ["ben", "cy"], "ben": ["dee", "eli"], "cy": ["fay"], "eli": ["gus"]}

everyone_under(reports, "ana")  ->  ["ben", "dee", "eli", "gus", "cy", "fay"]
everyone_under(reports, "ben")  ->  ["dee", "eli", "gus"]
everyone_under(reports, "fay")  ->  []

Constraints

  • Up to 10^5 employees; the chart can be deep (a long chain of single reports).

Clarifying questions

  • Is the input a map of direct reports, or a list of (employee, manager) pairs I must turn into one?
  • Should the manager be in the result?
  • Can the data contain a cycle or someone with two managers?

What a strong answer covers

  • Recursive depth-first search, then an iterative version with an explicit stack that pushes reports in reverse so the order matches.
  • O(size of the organization) time and O(depth) space; why recursion can overflow on a deep chain.
  • Building the reports map from (employee, manager) pairs when that is the input.
  • A visited set if the data might contain a cycle.

Follow-ups

  • The size of every manager's organization in one pass (post-order).
  • Print the chart indented by level.
  • Find the lowest common manager of two employees.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding