Loading...

The Hierarchy Builder

A hard Python interview practice problem on DataDriven. Write and execute real python code with instant grading.

Domain
Python
Difficulty
hard
Seniority
senior

Problem

The org chart is stored as flat parent-child pairs in a database export, and the frontend needs it as a nested JSON tree for the visualization component. Write a function that converts a flat list of (parent, child) tuples into a nested dictionary. There is exactly one root node (a parent that never appears as a child). Leaf nodes map to empty dicts. No cycles exist in the data.

Practice This Problem

Solve this Python problem with real code execution. DataDriven runs your Python code in a sandboxed Docker environment and grades it instantly.