PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates the ability to traverse and manipulate nested JSON-like structures, emphasizing concepts such as recursion or iterative traversal, array indexing, and path string construction; it tests data structures and serialization knowledge within the Coding & Algorithms domain and targets practical implementation skills for a software engineer role. It is commonly asked to assess how interviewees flatten hierarchical payloads for data processing or API normalization, reason about mixed object/array edge cases, and map hierarchical paths to primitive values.

  • medium
  • Salesforce
  • Coding & Algorithms
  • Software Engineer

Flatten a nested JSON object

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem You are given a JSON-like value representing an object that may contain: - **Objects / dictionaries** (string keys) - **Arrays / lists** - **Primitive values** (string/number/boolean/null) Return a **flattened map** from *path* to *primitive value*. ### Path rules - Use `.` to join object keys. - Use `[i]` for array indices. - The root has no leading separator. ### Examples **Input** ```json { "a": 1, "b": {"c": 2, "d": [3, 4]}, "e": [ {"f": 5}, 6 ] } ``` **Output** ```text a -> 1 b.c -> 2 b.d[0] -> 3 b.d[1] -> 4 e[0].f -> 5 e[1] -> 6 ``` ### Notes / constraints - Keys are strings and may contain any characters except `.` and `[` `]` (you may assume this to avoid escaping). - The output can be returned as a dictionary/map of `string -> primitive`. - Order does not matter. Implement a function to perform this flattening.

Quick Answer: This question evaluates the ability to traverse and manipulate nested JSON-like structures, emphasizing concepts such as recursion or iterative traversal, array indexing, and path string construction; it tests data structures and serialization knowledge within the Coding & Algorithms domain and targets practical implementation skills for a software engineer role. It is commonly asked to assess how interviewees flatten hierarchical payloads for data processing or API normalization, reason about mixed object/array edge cases, and map hierarchical paths to primitive values.

You are given a nested JSON-like object. Each value may be another dictionary, a list, or a primitive value. Flatten the structure into a dictionary that maps each primitive value to its full path. Use `.` between object keys and `[i]` for list indices. The root has no leading separator. Empty dictionaries and empty lists do not produce any entries.

Constraints

  • The root input is a dictionary.
  • 0 <= total number of dictionary entries and list elements <= 100000.
  • Keys are strings and do not contain `.`, `[` or `]`.
  • Values are dictionaries, lists, or primitive JSON-like values (`str`, `int`, `float`, `bool`, `None`).
  • Order of entries in the returned dictionary does not matter.

Examples

Input: ({"a": 1, "b": {"c": 2, "d": [3, 4]}, "e": [{"f": 5}, 6]},)

Expected Output: {"a": 1, "b.c": 2, "b.d[0]": 3, "b.d[1]": 4, "e[0].f": 5, "e[1]": 6}

Explanation: Each primitive value is mapped to its full path through nested dictionaries and lists.

Input: ({},)

Expected Output: {}

Explanation: An empty object has no primitive values, so the flattened result is empty.

Input: ({"x": None, "y": [True, {"z": -7}], "w": {"u": [False]}},)

Expected Output: {"x": None, "y[0]": True, "y[1].z": -7, "w.u[0]": False}

Explanation: The solution must correctly handle `None`, booleans, and negative numbers inside nested lists and dictionaries.

Input: ({"a": [[1, 2], [], [{"b": 3}]], "c": {}},)

Expected Output: {"a[0][0]": 1, "a[0][1]": 2, "a[2][0].b": 3}

Explanation: Nested lists can create paths with multiple index segments, and empty containers contribute no entries.

Input: ({"user": {"name": "Ana", "tags": ["dev", "ops"]}, "active": True},)

Expected Output: {"user.name": "Ana", "user.tags[0]": "dev", "user.tags[1]": "ops", "active": True}

Explanation: String values are primitives too, so they should appear in the flattened map.

Hints

  1. A depth-first traversal works well here: carry the current path as you move down the structure.
  2. When you move into a dictionary, append `.key` only if the current path is not empty; when you move into a list, append `[index]`.
Last updated: May 22, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Solve Two OA Coding Problems - Salesforce (medium)
  • Maximize events attended given date ranges - Salesforce (medium)
  • Implement common data-structure and JS tasks - Salesforce (medium)
  • Minimize operations to reduce integer to zero - Salesforce (medium)
  • Implement an LFU cache with O(1) operations - Salesforce (medium)