Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Flatten Nested Portfolio JSON

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

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

Goldman Sachs Asset & Wealth Management systems may represent complex portfolio holdings as nested JSON containing account metadata, security details, and arrays of positions. Write a function that flattens this structure into a dictionary whose keys are dot-delimited paths to leaf values.

Formal Specification

Implement flatten_holdings(holdings), where holdings is a JSON-compatible Python dictionary. Traverse nested dictionaries and lists recursively:

  1. Dictionary keys become path components.
  2. List positions become zero-based numeric path components.
  3. Scalar values, including strings, numbers, booleans, and None, become output values.
  4. An empty dictionary or list is treated as a leaf and retained in the output.
  5. Input object keys do not contain . characters, so dots can safely separate path components.

Return a dictionary mapping each leaf path string to its original value. The root input is always a dictionary. An empty root dictionary returns an empty result.

Constraints

  • 1 <= total number of JSON nodes <= 10^5
  • Maximum nesting depth is 1,000
  • Values are valid JSON types
  • Keys are non-empty strings without '.' characters
  • Each flattened path is unique

Function Signature

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