Unflatten dotted keys
Python data engineering interview problem. Difficulty: intermediate. Pattern: Recursion. About 16 minutes. Part of the Pro drill bank.
Reconstruct a nested dictionary and list structure from a flat dictionary of dotted path keys. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
Implement unflatten(flat: dict) -> dict to reverse a dotted key flattening operation. A flat dictionary contains keys like "a.b": 1 and "a.c.0": 2. Numeric path segments (such as "0", "1") represent zero-based list indices. Alphabetic string segments represent dictionary keys. Nested containers must be created as needed to place every value at its target path. An empty dictionary input must return {}.
Requirements
- Numeric segments must generate lists, not dicts.
- Do not mutate the input dictionary.
Constraints
- 0 <= len(flat) <= 1,000
- List indices are non-negative sequential integers.
Examples
Input: unflatten({'a.b': 1, 'a.c.0': 2, 'a.c.1': 3}) Output: {'a': {'b': 1, 'c': [2, 3]}} Path a.b becomes a nested dict entry, while a.c.0 and a.c.1 become a list.
Topics: lakebench, python, nested-dict, array-indices.
More Python interview questions · All interview problems · Learn data engineering
Unflatten dotted keys
Interview-style drill: Reconstruct a nested dictionary and list structure from a flat dictionary of dotted path keys.
Implement `unflatten(flat: dict) -> dict` to reverse a dotted key flattening operation. - A flat dictionary contains keys like `"a.b": 1` and `"a.c.0": 2`. - Numeric path segments (such as `"0"`, `"1"`) represent zero-based list indices. - Alphabetic string segments represent dictionary keys. - Nested containers must be created as needed to place every value at its target path. - An empty dictionary input must return `{}`.