Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Flatten Nested Config Dictionary

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

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

At Meta, infrastructure and service configs are often represented as nested JSON-like dictionaries. Write a function that flattens a nested dictionary into a single-level dictionary using dot-separated key paths.

Given a dictionary where values may be primitives or other dictionaries, return a new dictionary mapping each leaf value to its full path. If a value is not a dictionary, it is a leaf and should appear in the output. Use . to join nested keys.

Formal Specification

  • Input: obj, a Python dictionary with string keys. Values may be strings, numbers, booleans, null-like values, lists, or nested dictionaries.
  • Output: A new dictionary where each key is the full dot-separated path to a leaf value, and each value is the original leaf value.
  • Empty dictionaries should contribute no entries.

Constraints

  • 0 <= len(obj) <= 10^4 total key-value pairs across all nested dictionaries
  • Keys are non-empty strings and do not contain '.'
  • Nesting depth is at most 10^3
  • Values may be primitives, lists, null-like values, or nested dictionaries
  • Empty dictionaries should produce no flattened entry

Function Signature

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