Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.