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
Get started
Flatten Nested Arrays or Objects
00:00
5 left

Flatten Nested Arrays or Objects

MediumPython

Problem

Walmart Global Tech frontend services often transform nested catalog and fulfillment data before rendering it. Implement flatten_data to convert a nested JSON-like object or array into a single dictionary whose keys represent each value's path.

Formal Specification

The input is a non-empty JSON-like dictionary or list. Dictionary keys contain no periods. Values may be dictionaries, lists, strings, numbers, booleans, or None.

Use dot notation for dictionary keys and zero-based numeric components for list indices. Each scalar value becomes an entry in the output dictionary. Preserve empty dictionaries and lists as values at their own path. For a root scalar, the path is the empty string, although root scalars are excluded by the constraints.

For example, {"item": {"sku": "A1"}} becomes {"item.sku": "A1"}, while {"items": [{"id": 7}]} becomes {"items.0.id": 7}.

Constraints

  • 1 <= total number of containers and scalar values <= 10^4
  • Maximum nesting depth is 1000
  • Dictionary keys are non-empty strings containing no periods
  • Input contains only JSON-compatible values
  • The root input is a dictionary or list

Function Signature

def flatten_data(data):
Interviewer

Your question is Flatten Nested Arrays or Objects. 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.