Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
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
Dynamic Dictionary Sampling Script
00:00
5 left

Dynamic Dictionary Sampling Script

MediumPython

Problem

Write a Python script to perform dynamic insertion, removal, and unbiased random sampling on dynamic dictionary structures.

Implement dynamic_dictionary(operations, seed) using operations ["insert", key, value], ["remove", key], and ["sample"]. Return one result per operation: insertion returns whether the key was new, removal returns whether the key existed, and sampling returns a uniformly random current key or None when empty. Updating an existing key must not create a duplicate sampling entry. Use seed only to make tests reproducible.

Example: [["insert", "a", 1], ["insert", "b", 2], ["remove", "a"], ["sample"]] with seed 4 returns [true, true, true, "b"].

Constraints: At most 1,000 operations, string keys are unique dictionary keys, and values are arbitrary Python values.

Constraints

  • 1 <= len(operations) <= 1,000
  • Each operation is either ["insert", key, value], ["remove", key], or ["sample"]
  • Keys are hashable and values are arbitrary Python values
  • Sampling must be uniform over active keys
  • Updating an existing key does not add another sampling entry

Function Signature

def dynamic_dictionary(operations, seed):
Interviewer

Your question is Dynamic Dictionary Sampling Script. 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.