# The Config Blender

> Config collision. The surviving values after a merge.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given two dicts base and override, recursively deep-merge them. For any key, if both sides have dict values, recurse. Otherwise the override value wins. Do not mutate inputs.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **recursive dict merging**, a pattern that appears in configuration management, feature flags, and infrastructure-as-code. It probes whether a candidate can write a recursive function that correctly handles nested structures without clobbering unrelated keys.

---

### Break down the requirements

#### Step 1: Iterate through keys in the override dict

For each key, decide whether to recurse deeper or overwrite.

#### Step 2: Recurse when both values are dicts

If a key exists in both dicts and both values are dicts, merge them recursively. Otherwise, the override value wins.

#### Step 3: Preserve base keys not in override

Start with a copy of the base dict so keys absent from the override survive.

---

### The solution

**Recursive deep merge with override precedence**

```python
def deep_merge(base: dict, override: dict) -> dict:
    merged = dict(base)
    for key, value in override.items():
        if key in merged and isinstance(merged[key], dict) and isinstance(value, dict):
            merged[key] = deep_merge(merged[key], value)
        else:
            merged[key] = value
    return merged
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the total number of keys across all nesting levels in both dicts.
> 
> **Space:** O(n) for the merged output. Recursion depth equals the maximum nesting depth.

> **Interviewers Watch For**
>
> Whether you create a new dict instead of mutating the base. Mutating the input is a common production bug that causes side effects in callers who still reference the original config.

> **Common Pitfall**
>
> Using `dict.update()` at the top level. This clobbers nested dicts entirely instead of merging their contents.

---

## Common follow-up questions

- What if the nesting depth could be very large? _(Tests awareness of Python's recursion limit and iterative alternatives using a stack.)_
- How would you handle list values? _(Tests design decision: concatenate, replace, or merge by index?)_
- What if you need a three-way merge (base, env, local)? _(Tests chaining: `deep_merge(deep_merge(base, env), local)`.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_config_blender)
- [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.