Transform Dotted Keys into a Nested Map
Updated
Problem statement
Given parallel arrays of dotted keys and compact JSON object strings, construct the corresponding nested object. Split every key on dots, create the path of nested objects, and preserve each supplied JSON value exactly at its leaf.
Return one compact JSON object string. Sort keys lexicographically at every nesting level so the result is deterministic.
Function
nestDottedMap(dottedKeys: String[], jsonValues: String[]) → StringExamples
Example 1
dottedKeys = ["a.bbb.c","a.bbb.d","a.cc","a.cd.d.e","b.a"]jsonValues = ["{\"data\":1}","{\"data\":5}","{\"data\":2}","{\"data\":3,\"foo\":4}","{\"data\":7}"]return = "{\"a\":{\"bbb\":{\"c\":{\"data\":1},\"d\":{\"data\":5}},\"cc\":{\"data\":2},\"cd\":{\"d\":{\"e\":{\"data\":3,\"foo\":4}}}},\"b\":{\"a\":{\"data\":7}}}"The paths under a.bbb share one nested object, while b.a creates a separate top-level branch. Keys are emitted in lexicographic order.
Example 2
dottedKeys = ["z","a.b"]jsonValues = ["{\"v\":2}","{\"v\":1}"]return = "{\"a\":{\"b\":{\"v\":1}},\"z\":{\"v\":2}}"The top-level key a precedes z, regardless of input order.
Constraints
- The input contains between 1 and 2000 dotted keys and the same number of JSON values.
- Each dotted key contains between 1 and 200 characters, uses non-empty alphanumeric or underscore segments, is unique, and is not a prefix of another key.
- Each value is a valid compact JSON object string containing between 2 and 1000 characters; its original bytes must be preserved.
- The returned JSON object orders every level's keys lexicographically.
Source note: Author-written prompt excerpt retained for source fidelity.
More Proofpoint interview questions
Similar questions at other companies
- Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Highlight and Rank Overlapping CitationsHarvey · ONSITE INTERVIEWHarvey interview questions
- Number of AtomsAmazon · ONSITE INTERVIEW
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Common Words Across FilesMeta · PHONE SCREENMeta interview questions
- Indexed API Field Pattern QueriesStripe · PHONE SCREENStripe interview questions
- Summarize Server Healthcheck LogsNVIDIA · PHONE SCREENNVIDIA interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions