InterviewDB Experience

Print Employee Hierarchy as a Tree: Render a Manager-Report Org Chart in ASCII Format

Interview Experience

Problem

You are given a list of (employee, manager) pairs representing a company org chart. The root node has no manager. Print the hierarchy as an indented ASCII tree where each level of depth adds 2 spaces.

python
def print_hierarchy(pairs: list[tuple[str, str]]) -> None:
    pass

Example:

pairs = [("Bob","Alice"),("Carol","Alice"),("Dave","Bob"),("Alice",None)]

**output**:
Alice
  Bob
    Dave
  Carol

Approach

Build an adjacency list (parent -> children). DFS from the root, passing current depth to compute indent. Sort children alphabetically for determinism.

Follow-ups
1. What if the input contains a cycle (data error)? How do you detect and report it?
2. The company has 1 million employees. The recursion overflows the call stack. How do you convert the DFS to iterative?
3. Add the ability to print only the subtree rooted at a given employee.
4.

Output valid JSON instead of ASCII, preserving the nested hierarchy structure.

Full Details

Problem

You are given a list of (employee, manager) pairs representing a company org chart. The root node has no manager. Print the hierarchy as an indented ASCII tree where each level of depth adds 2 spaces.

python
def print_hierarchy(pairs: list[tuple[str, str]]) -> None:
    pass

Example:

pairs = [("Bob","Alice"),("Carol","Alice"),("Dave","Bob"),("Alice",None)]

**output**:
Alice
  Bob
    Dave
  Carol

Approach

Build an adjacency list (parent -> children). DFS from the root, passing current depth to compute indent. Sort children alphabetically for determinism.

Follow-ups
1. What if the input contains a cycle (data error)? How do you detect and report it?
2. The company has 1 million employees. The recursion overflows the call stack. How do you convert the DFS to iterative?
3. Add the ability to print only the subtree rooted at a given employee.
4.

Output valid JSON instead of ASCII, preserving the nested hierarchy structure.

About This Question

This is a candidate experience report from a chime interview during the onsite round.

It covers the following topics: Graph, Recursion, Coding, Onsite, Stack .