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
Traverse and Transform Nested JSON
00:00
5 left

Traverse and Transform Nested JSON

MediumPython

Problem

lululemon product attributes and order metadata can contain deeply nested JSON objects and arrays. Implement an algorithm that transforms this JSON into a flat dictionary containing one entry for every non-null primitive value.

Formal Specification

Implement flatten_metadata(data), where data is a JSON-compatible object containing dictionaries, lists, strings, numbers, booleans, and null. Return a dictionary mapping each leaf's path to its primitive value.

Use these path rules:

  1. Dictionary keys are separated by ..
  2. Array elements use zero-based bracket notation, such as items[0].sku.
  3. In dictionary keys, escape \\ as \\\\ and . as \\. before joining paths.
  4. Omit null values and empty dictionaries or lists.
  5. The input root is always a dictionary. Dictionary key order does not affect correctness.
  6. The algorithm must use an explicit stack rather than Python recursion so that deeply nested metadata does not overflow the call stack.

Constraints

  • 1 <= total JSON nodes <= 2 * 10^5
  • Maximum nesting depth is 10^4
  • Dictionary keys are non-empty strings
  • Leaf values are strings, numbers, booleans, or null
  • The input contains no cyclic references

Function Signature

def flatten_metadata(data):
Interviewer

Your question is Traverse and Transform Nested JSON. 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.