Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Merge Dictionaries in Python

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Merge Dictionaries in Python. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

ADP Workforce Now may combine default and employee-specific configuration values before processing a request. Implement a function that merges two Python dictionaries into a new dictionary.

If both dictionaries contain the same key, the value from updates must override the value from defaults. Do not modify either input dictionary. This is a shallow merge, so nested dictionaries should be treated as ordinary values rather than merged recursively.

Formal Specification

Implement merge_dicts(defaults, updates).

  • Input: Two dictionaries whose keys are hashable Python values and whose values may be any Python objects.
  • Output: A new dictionary containing every key from both inputs.
  • Collision rule: For duplicate keys, use the value from updates.
  • Mutation rule: Leave defaults and updates unchanged.

Constraints

  • 0 <= len(defaults), len(updates) <= 10^5
  • Keys are valid Python dictionary keys
  • The result contains at most len(defaults) + len(updates) distinct keys
  • The input dictionaries must not be modified

Function Signature

def merge_dicts(defaults, updates):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output