KatInterview

Print a Reporting Hierarchy and List Two-Level-Down Pairs

Trees & GraphsModel answer
MediumModel answerTrees & Graphstreepreorderiterative-dfs

Last updated

Turn manager rows into a tree, infer the root, print it with depth-based dot indentation, or list every (manager, employee) pair exactly two levels apart.

You are given the reporting structure of a company as a list of rows. Each row has the form [manager, report1, report2, ...]. The reports are listed in the order they should be displayed. Implement:

org_chart(relations: string[][], mode: int) -> string[]

Mode 0 (print the hierarchy). Return one string per employee in preorder: an employee, then each of its subtrees in the stored order. Each line is the employee name preceded by four . characters for every level of depth. The root is at depth 0 and has no prefix.

Mode 1 (two-level-down pairs). Return strings of the form manager,employee (one comma, no spaces) for every pair where employee is exactly two edges below manager. Not one edge, and not three or more.

  • Process managers in tree preorder.
  • For one manager, go through its direct reports in order, and for each of them go through that report's own reports in order.

Rules and constraints

  • Names are nonempty ASCII strings made of letters, digits, and underscores.
  • A manager is the head of at most one row, and no row lists the same report twice.
  • The rows describe a single valid rooted tree of at most 2,000 employees: no cycles, exactly one parent for every non-root employee, exactly one root. The root is not given, so you must infer it.
  • A row with one element (such as ["B"]) is an employee with no reports. Employees who only ever appear as reports are leaves and need no row of their own.
  • An empty relations list means an empty chart, so return [] in both modes.
  • Keep the input order of reports. Do not sort.

Examples

relations = [["A","B","C"],["B","E"],["C","D"]]
mode = 0
result = ["A","....B","........E","....C","........D"]
relations = [["A","B","C"],["B","E"],["C","D"]]
mode = 1
result = ["A,E","A,D"]

What to deliver

Write the function, then discuss the time and space cost of building the tree, printing it, and listing the pairs, including the size of the returned strings. Describe how you would test a single-root chart, a chain, and a branching tree.

Hints

Hint 1

The root is the one employee who is the head of a row but never appears in anyone's report list. Collect all report names into a set while you build the manager-to-reports map.

Hint 2

Printing needs absolute depth from the root, so carry a depth counter in the traversal. Pair listing only needs relative distance: from each manager, look at its reports, then at their reports. The manager's own depth is irrelevant.

Hint 3

A chain of 2,000 employees would overflow a naive recursive DFS in many languages, so use an explicit stack and push children in reverse order to keep input order. Also notice that printing a chain produces about 2n² characters, which is a property of the output itself.