# The Deep Config

> Nested config, dot-notation output.

Canonical URL: <https://datadriven.io/problems/the_deep_config>

Domain: Python · Difficulty: medium · Seniority: L5

## Problem

Given a nested dict, flatten it by joining keys at each level with '.'. Values (non-dict leaves) become entries in a flat dict with dotted keys.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **recursive dict flattening with key concatenation**, a pattern used in configuration management, logging, and feature flag systems. It probes recursion, string building, and base case identification.

---

### Break down the requirements

#### Step 1: Iterate through dict keys

For each key, check whether the value is a dict (recurse) or a leaf (output).

#### Step 2: Build dot-separated key paths

Concatenate parent keys with dots as you descend. The prefix parameter tracks the path so far.

#### Step 3: Handle the base case

When a value is not a dict, add the full dot-separated key and its value to the result.

---

### The solution

**Recursive flattening with prefix tracking**

```python
def flatten_config(d: dict, prefix: str = "") -> dict:
    result = {}
    for key, value in d.items():
        if prefix:
            full_key = prefix + "." + key
        else:
            full_key = key
        if isinstance(value, dict):
            nested = flatten_config(value, full_key)
            for nk, nv in nested.items():
                result[nk] = nv
        else:
            result[full_key] = value
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the total number of key-value pairs across all nesting levels.
> 
> **Space:** O(n) for the flattened output. Recursion depth equals the maximum nesting level.

> **Interviewers Watch For**
>
> Whether you handle the empty prefix correctly. The root level should not have a leading dot in the key path.

> **Common Pitfall**
>
> Using string concatenation with `+` in the prefix creates many intermediate strings. For very deep nesting, consider using a list and `'.'.join()` at the leaf.

---

## Common follow-up questions

- How would you unflatten the dict back to nested form? _(Tests splitting dot-separated keys and building nested dicts.)_
- What if values can also be lists? _(Tests design decision: flatten list items with numeric indices or leave lists as leaf values.)_
- What if keys themselves contain dots? _(Tests escaping or using a different separator.)_
- How would you handle this iteratively? _(Tests using an explicit stack instead of recursion.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_deep_config)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.