Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse Array or Flatten Object

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

Your question is Reverse Array or Flatten Object. 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

Celebal Technologies frontend dashboards receive configuration data as nested JSON-like objects. Implement flatten_config to convert every nested dictionary or list value into a single dictionary whose keys use dot-separated paths.

Formal Specification

The input is a JSON-like Python value containing dictionaries, lists, and primitive values: strings, numbers, booleans, or None. Dictionary keys are non-empty strings that do not contain a dot. A dictionary child uses its key in the path, and a list child uses its zero-based index. Return a dictionary mapping each leaf path to its original value.

Non-empty dictionaries and lists must be recursively expanded. Empty dictionaries and empty lists are treated as leaf values and should be preserved. The root input is always a dictionary. Do not mutate the input.

Constraints

  • 0 <= number of containers and leaves <= 10^4
  • Maximum nesting depth is 100
  • Dictionary keys are non-empty strings without a dot
  • Values are JSON-like: dictionaries, lists, strings, numbers, booleans, or None
  • The root input is always a dictionary
  • The input must not be mutated

Function Signature

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