Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Flatten Nested Dictionaries

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

Your question is Flatten Nested Dictionaries. 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

Patreon integrations may represent creator, membership, and campaign metadata as deeply nested dictionaries. Implement flatten_metadata to traverse every nested dictionary and return a single-level dictionary whose keys contain the full path, joined with ..

Formal Specification

Given metadata, a dictionary with string keys and JSON-compatible values, return a dictionary mapping each leaf path to its value. A value is a leaf when it is not a dictionary, including lists, strings, numbers, booleans, and None. Empty dictionaries produce no output. Input keys do not contain ..

The function must not mutate metadata. Output key order is not significant.

Constraints

  • 0 <= total number of dictionary entries <= 10^5
  • Maximum nesting depth is 10^4
  • Every key is a non-empty string and does not contain .
  • Values are JSON-compatible
  • The input dictionary must not be mutated

Function Signature

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