FastPrepFlatten Nested Map Paths with a Stack
Problems/Flatten Nested Map Paths with a Stack

Flatten Nested Map Paths with a Stack

Salesforce logoSalesforce● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report explicitly asks for a stack that prints every parent key for nested map values and follows up with list-valued leaves. The token-stream adapter directly exposes those requirements and matches at about 95%.

tokens encode a nested ordered map:

  • key{ opens a nested map under key.
  • } closes the current map.
  • key=value is a leaf; value may be a scalar or a bracketed list such as [x,y].

Return every leaf in encounter order as parent.child.key=value, including all parent keys.

Function

flattenNestedMap(tokens: String[]) → String[]

Examples

Example 1

tokens = ["a{","b{","c=1","}","d=2","}"]return = ["a.b.c=1","a.d=2"]

The path stack contains a,b for c and only a for d.

Example 2

tokens = ["root{","items=[x,y]","}"]return = ["root.items=[x,y]"]

A list value is retained as one leaf value.

Example 3

tokens = ["x=1","y=2"]return = ["x=1","y=2"]

Top-level leaves have no parent prefix.

Constraints

  • 1 <= tokens.length <= 10^5.
  • The token stream is balanced and valid; keys are nonempty and contain no dots or delimiters.
  • Leaf values are nonempty and contain no equals sign.

More Salesforce problems

  • Count Subarrays With Product Equal to KOA · Seen Nov 2026
  • Minimum No-Repeat Segments After One Character RemovalOA · Seen Nov 2026
  • Get Message StatusOA · Seen Nov 2026
All Salesforce interview questionsSee Salesforce hiring insights
Saved
class Solution {
    public String[] flattenNestedMap(String[] tokens) {
        // Write your solution here.
    }
}
Checking account…