Dataford
Interview QuestionsInterview GuidesExperiencesCoachingPricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Merge Dictionaries in Python
00:00
5 left

Merge Dictionaries in Python

EasyPython

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):
Interviewer

Your question is Merge Dictionaries in Python. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.