Serialize a Nested Dictionary as JSON
Updated
Problem statement
Serialize a nested value containing strings, lists, and string-keyed dictionaries into a compact JSON string.
The value is encoded by nodes, and row 0 is the root dictionary:
["STRING", value]is a string node.["LIST", child1, child2, ...]is a list whose remaining fields are child-node indexes in order.["DICT", key1, child1, key2, child2, ...]is a dictionary whose remaining fields are key and child-index pairs in insertion order.
Child indexes always point to later rows, and every row is reached exactly once. Escape quotation marks, backslashes, newline, carriage return, and tab inside keys and string values. Return JSON without extra whitespace while preserving list and dictionary order.
Function
serializeNestedDictionary(nodes: String[][]) → StringExamples
Example 1
nodes = [["DICT","a","1","c","2"],["STRING","b"],["STRING","d"]]return = "{\"a\":\"b\",\"c\":\"d\"}"The root dictionary contains two string-valued entries in their encoded order.
Example 2
nodes = [["DICT","items","1"],["LIST","2","3"],["STRING","x"],["DICT","k","4"],["STRING","line\nnext"]]return = "{\"items\":[\"x\",{\"k\":\"line\\nnext\"}]}"The newline inside the nested string is escaped as \n.
Constraints
1 <= nodes.length <= 100000.- Row
0is a validDICTrow. - Every child index is a decimal integer naming a later row.
- Every dictionary key is unique within its dictionary.
- Each row is reachable exactly once, so the encoded value is a tree.
- The total output length is at most
200000.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Business Account KYC Verification — Parts 1–5Stripe · PHONE SCREENStripe interview questions
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Parse Nested Message TypesApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Positive-Integer Expression EvaluationJane Street · PHONE SCREENJane Street interview questions
- CSV Row SerializerMercor · PHONE SCREENMercor interview questions
- Host Log ParserVerkada · PHONE SCREENVerkada interview questions
- Length-Prefixed String CodecSnowflake · PHONE SCREENSnowflake interview questions
- Recursive Nested JSON Key SearchMongoDB · PHONE SCREENMongoDB interview questions