Back to Practice
#0052

Merge Default and User Config

EasyPython12 min10 XP

Problem

Given two dictionaries, return a new dictionary containing all default values, but with user values overriding matching keys.

Why This Matters

Config merging is common in scripts, model training jobs, APIs, dashboards, and CLI tools. The interview trap is accidentally mutating shared defaults.

Function Signature

def merge_config(defaults, user):

Examples

Example 1
Inputdefaults = {"theme": "dark", "limit": 20}, user = {"limit": 50}
Output{"theme": "dark", "limit": 50}

The user override replaces limit, while theme comes from defaults.

Constraints

  • Return a new dictionary.
  • Do not mutate defaults or user.
  • Only shallow merging is required.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Override one key
Input{"theme": "dark", "limit": 20}, {"limit": 50}
Expected{"theme": "dark", "limit": 50}

User settings should win when keys overlap.

Do not mutate inputs
Inputdefaults and user dictionaries
Expectedoriginal dictionaries unchanged

Shared defaults should remain safe for future calls.

Hidden Test Categories
Empty user configEmpty defaultsAll keys overridden