Deep-Merge Two JSON Configs

Hard ⏱ 18 min 30% acceptance ★★★★★ 4.8
Write merge(base, override) deep-merging two parsed JSON dicts: when both hold a dict at the same key, merge recursively; otherwise override wins. Neither input may be mutated — return a new dict.

Examples

Example 1
Input
merge({'db': {'host': 'a', 'port': 5432}}, {'db': {'host': 'b'}})
Output
{'db': {'host': 'b', 'port': 5432}}
Explanation

host overridden, port inherited.

Constraints

  • Recursive dict-vs-dict merging.
  • Inputs untouched (copy as you go).

Topics

JSONdeep merge

Companies

HashiCorpDockerRed Hat

Hints

Hint 1

Start from dict(base) and lay override on top.

Hint 2

Recurse only when both sides are dicts.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.