FastPrepFlatten Nested Map Paths with a Stack

Flatten Nested Map Paths with a Stack

Salesforce logoSalesforceMediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

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

See Salesforce hiring insights
public String[] flattenNestedMap(String[] tokens) {
    // Write your solution here.
}
tokens["a{","b{","c=1","}","d=2","}"]
expected["a.b.c=1", "a.d=2"]
Checking account…