Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Flatten Nested Data

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

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

LinkedIn frontend surfaces often receive deeply nested JSON-like data for profiles, jobs, and recommendations. Implement a function that flattens nested dictionaries and arrays into a single dictionary whose keys represent each value's path.

Formal Specification

Given data, a JSON-like dictionary or array, return a dictionary mapping dot-delimited path strings to scalar values. Dictionary keys become path components, and array positions become zero-based numeric components. A scalar is any value that is not a dictionary or array, including null and booleans. Empty dictionaries and arrays should be preserved as values at their paths. Input keys do not contain .. The root input is always a dictionary or array.

Use an iterative depth-first traversal so the function does not depend on Python's recursion limit.

Constraints

  • The input contains at most 10^5 total dictionaries, arrays, and scalar values.
  • Nesting depth can exceed 1,000.
  • Dictionary keys are strings and do not contain ..
  • Values are valid JSON-like values: dictionaries, arrays, strings, numbers, booleans, or null.
  • The root input is always a dictionary or array.

Function Signature

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