Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Recursion Over Nested Data

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

Your question is Recursion Over 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

Klaviyo event payloads can contain nested objects and arrays, such as profile properties, product details, and line items. Implement a recursive function that flattens a JSON-compatible payload into a dictionary of paths to scalar values.

Formal Specification

Given payload, a JSON-compatible Python dictionary or list, return a dictionary where:

  1. Each scalar value, including strings, numbers, booleans, and None, becomes one output entry.
  2. Nested dictionary keys are joined with ..
  3. Array elements use zero-based numeric path components.
  4. Empty dictionaries and lists produce no entries.
  5. The input must not be modified.

For example, {"customer": {"email": "a@example.com"}} becomes {"customer.email": "a@example.com"}.

Constraints

  • 1 <= number of contained values <= 10^4
  • Nesting depth is at most 100
  • Dictionary keys are non-empty strings and do not contain .
  • Values follow standard JSON types: object, array, string, number, boolean, or null

Function Signature

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