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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
Create a Nonmutating Shallow Dictionary Merge
00:00
5 left

Create a Nonmutating Shallow Dictionary Merge

EasyPython

Problem

ADP Workforce Now may combine default and employee-specific configuration values before processing a request. Implement a function that merges two 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 values and whose values may be any 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 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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.