L2 · Working engineerDictionaries & hashing~4 min · 4 tests#31

Merge Two Dictionaries in Python

Merge two Python dictionaries into a new one without mutating either, with the second dict winning conflicts. Covers |, ** unpacking and update().

The problem

Return a new dictionary containing every key from d1 and d2. If a key is in both, the value from d2 wins. Neither input may be modified.

Examples

  1. Example 1

    Input

    merge_dicts({'a': 1, 'b': 2}, {'c': 3, 'd': 4})

    Expected output

    {'a': 1, 'b': 2, 'c': 3, 'd': 4}
  2. Example 2

    Input

    merge_dicts({'a': 1}, {'a': 9})

    Expected output

    {'a': 9}

+ 2 hidden tests on Submit — inputs not mutated.

Edge cases to ask about

  • Overlapping keys
  • Empty dicts
  • Inputs must not change

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · merge_dicts
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and read why.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Copy + update()O(n + m)O(n + m)Works on every Python 3 version.
    {**d1, **d2}O(n + m)O(n + m)Unpacking, Python 3.5+.
    bestd1 | d2O(n + m)O(n + m)The merge operator, Python 3.9+.
    Walkthrough of the optimal approach (try it yourself first)

    Copy d1 (so it is not mutated) and update with d2; later keys overwrite earlier ones. {**d1, **d2} and d1 | d2 do the same thing — knowing which Python version introduced each is a common follow-up.

    Complexity: O(n + m) time, O(n + m) space. Every key from both dicts is copied once into the new dict.

    Reveal the reference solution
    def merge_dicts(d1, d2):
        merged = dict(d1)
        merged.update(d2)
        return merged

    Follow-ups interviewers ask

    • Deep-merge nested dicts.
    • Sum values on conflicting keys.

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Merge Two Dictionaries in Python?

    The optimal solution runs in O(n + m) time and O(n + m) auxiliary space. Every key from both dicts is copied once into the new dict.

    What is the brute-force approach, and how do you optimise it?

    Copy + update(): O(n + m) time, O(n + m) space. Works on every Python 3 version. {**d1, **d2}: O(n + m) time, O(n + m) space. Unpacking, Python 3.5+. d1 | d2: O(n + m) time, O(n + m) space. The merge operator, Python 3.9+.

    What follow-up questions do interviewers ask about Merge Two Dictionaries in Python?

    Deep-merge nested dicts. Sum values on conflicting keys.