N-Ary Tree BFS Codec
Updated
Problem statement
You will build both halves of a codec for ordered N-ary trees. One half turns a tree into a string. The other half turns that string back into a tree. Node values are signed integers. Go through the rows of operations in order and return one result per row.
["SERIALIZE", tree]: turn the tree text into the level-by-level string described below.["DESERIALIZE", data]: turn a valid level-by-level string back into tree text.
Tree text
An empty tree is the empty string. A leaf is just its value, written as a decimal number. Any other node is its value followed by its children inside parentheses, separated by commas. The order of the children matters. For example, 1(3(5,6),2,4) has root 1, and its children are 3, 2, and 4.
Level-by-level string
An empty tree is again the empty string. Otherwise the string starts with the root value. Each later level comes after a |. To write a later level, go through every node on the level above, from left to right. For each of them, list its children's values from left to right, then write # to end that node's list. Leave out a final level that would hold only # markers.
For example, 1(3(5,6),2,4) becomes 1|3,2,4,#|5,6,#,#,#. In the last level, the first # ends the children of node 3. The other markers show that nodes 2 and 4 have no children.
Function
transformNaryBfsCodec(operations: String[][]) → String[]Examples
Example 1
operations = [["SERIALIZE","1(3(5,6),2,4)"],["DESERIALIZE","7|8,9,10,#|#,11,#,#"]]return = ["1|3,2,4,#|5,6,#,#,#","7(8,9(11),10)"]The first operation writes the tree level by level and uses # to close each parent's child list. In the second operation, node 9 is the only child on its level that receives a child.
Example 2
operations = [["SERIALIZE","42"],["DESERIALIZE","-1|2,3,#"]]return = ["42","-1(2,3)"]A single node needs no later level. The second level-by-level string gives the root two leaf children.
Constraints
1 <= operations.length <= 200.- Every operation has exactly two strings. The first one is
SERIALIZEorDESERIALIZE. - Every tree text and level-by-level string in the input is valid.
- Each tree has at most
5000nodes. - Every node value fits in a signed 32-bit integer.
- All operations together have at most
20000nodes.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Serialize and Deserialize a Binary TreeAmazon · ONSITE INTERVIEW
- N-Ary Tree Codec OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- JSON Schema Object ValidatorDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Spreadsheet Formula DependenciesAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Serialize and Deserialize a Binary TreeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Serialize and Deserialize an N-Ary TreeUber · ONSITE INTERVIEWUber interview questions
- Parse Nested HTML into a TreeApple · ONSITE INTERVIEWApple interview questions
- Dictionary Records to an Editable XML TreeSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions