Render an Organization Chart and Find Skip-Level Pairs
SnapchatMachine Learning Engineer · OnsiteLast updated
Build and render an organization tree with ordered indentation, infer its root, and return exactly two-level manager-descendant pairs.
Build an organization tree from manager/direct-report rows. Support rendering the tree with indentation and listing all skip-level pairs, where the second employee is exactly two edges below the first.
Implement org_chart(relations: string[][], mode: int) -> string[]:
- A row
[manager, child1, child2, ...]lists that manager's direct reports in display order. mode == 0: return the rendered lines in preorder. Prefix each employee with four dots per depth level; the root has no prefix.mode == 1: return skip-level pairs encoded asmanager,employee, with one comma and no spaces. Visit managers in tree preorder; for each manager, visit its children and then each child's children in the stored order.
Constraints & Assumptions
- Each employee name is a nonempty ASCII string of letters, digits, or underscores.
- Each manager appears as the first item of at most one row, and no direct-report list contains a duplicate.
- The rows describe one valid rooted tree with at most 2,000 employees: no cycles, one parent per non-root employee, and exactly one root. The root is not separately supplied.
- A one-element row can represent an employee with no reports. An empty relation list represents an empty chart and returns an empty list in either mode.
- Employees that appear only as children are leaves. Preserve input child order; do not alphabetize it.
- The mode interface and pair serialization are explicit practice encodings of the reported class methods. Skip-level means exactly two levels, not all descendants beyond one level.
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"]
Explain the time and space costs of building the tree, rendering it, and listing skip-level pairs, including the size of the returned strings. Describe tests for a single root, a chain, and a branching tree.
Hints
Hint 1
Separate depth from skip-level distance: Rendering uses each node's depth from the root. A skip-level pair uses only a manager's children and their children, regardless of that manager's absolute depth.
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.