Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Flatten Nested Portfolio JSON
00:00
5 left

Flatten Nested Portfolio JSON

MediumPython

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):
Interviewer

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