Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Flatten Deeply Nested Objects

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

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

ThoughtSpot frontend surfaces often pass deeply nested configuration objects between components. Implement flatten_object to convert a nested object into a single-level object whose keys represent paths from the root.

A nested object key should be joined to its parent path with .. Treat arrays and primitive values as leaf values and do not flatten objects inside arrays. Empty objects are also leaf values. The input object and all nested keys are JSON-compatible, and no key contains ..

Formal Specification

  • Input: obj, a non-null Python dictionary whose values are dictionaries, arrays, strings, numbers, booleans, or None.
  • Output: A dictionary mapping each leaf path to its original value.
  • The root object itself does not contribute a key prefix.
  • Return {} for an empty input object.
  • Use an iterative approach so deeply nested objects do not depend on Python's recursion limit.

Constraints

  • 1 <= total number of object entries <= 10^5
  • Maximum nesting depth is 10^5
  • Keys are non-empty strings and do not contain .
  • Values are JSON-compatible
  • Arrays are treated as atomic values and are not traversed

Function Signature

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