# Coalesce Fields

> Nulls are hiding. Fill them in.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given primary and defaults dicts, return a merged dict where every key in primary is kept, but if primary[key] is None it is replaced by defaults[key] (if the key exists in defaults). Keys only in defaults are NOT added.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **dictionary merging with null-coalescing logic**, a pattern that appears in config loaders, API response normalization, and ETL field mapping. It checks whether you understand `None` vs. missing keys.

---

### Break down the requirements

#### Step 1: Start with all keys from defaults

Copy the defaults dict as the base, so keys present only in defaults are included automatically.

#### Step 2: Override with non-None values from primary

For each key in the primary dict, use the value only if it is not None. If the value is None, keep the default.

#### Step 3: Include primary-only keys

Keys that exist in primary but not in defaults should also appear in the output, even if their value is None.

---

### The solution

**Two-pass merge with null coalescing**

```python
def coalesce(primary, defaults):
    result = {}
    for key in defaults:
        result[key] = defaults[key]
    for key in primary:
        if primary[key] is not None:
            result[key] = primary[key]
        elif key not in result:
            result[key] = primary[key]
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(d + p) where d is the number of default keys and p is the number of primary keys. Each dict is iterated once.
> 
> **Space:** O(d + p) for the merged result dict.

> **Interviewers Watch For**
>
> Whether you distinguish between a key being absent and a key having the value `None`. Using `is not None` (not truthiness) is critical, since `0`, `''`, and `False` are valid non-null values.

> **Common Pitfall**
>
> Using `if primary[key]` instead of `if primary[key] is not None`. This incorrectly treats `0`, empty strings, and `False` as null values.

---

## Common follow-up questions

- What if the merge should be recursive for nested dicts? _(Tests deep merge patterns where nested dicts at the same key should be merged rather than replaced.)_
- How would you handle a chain of three or more config layers? _(Tests whether you can generalize to N-layer merging by folding left to right.)_
- What if None values in primary should explicitly override defaults to None? _(Tests understanding of different null semantics: coalesce vs. explicit null.)_

## Related

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