N-Ary Tree Codec Operations
Updated
Problem statement
You are given a list of operations on trees. Each tree is an N-ary tree: every node holds a nonnegative integer, and a node can have any number of children, kept in a fixed order. Some operations turn a tree into a string, and others turn that string back into the tree. Return one result for each operation, in the same order as the operations.
Tree text
A leaf is written as just its value in decimal digits. A node with children is written as its value, then its children inside parentheses, separated by commas. The order of the children matters. For example, 1(2,3(4,5)) has root 1. The root has a leaf child 2 and a child 3 whose own children are 4 and 5.
Wire format
To build the wire format, visit the nodes in preorder: a node first, then the subtree of each of its children from left to right. Write each node as value:childCount, which is its value and its number of children. Join these pieces with |. The example above becomes 1:2|2:0|3:2|4:0|5:0.
Operations
["SERIALIZE", treeText]: read the tree text and return the tree in wire format.["DESERIALIZE", wire]: rebuild the tree from the wire format and return its tree text, written exactly as described above.
Every input string is valid.
Function
transformNaryCodec(operations: String[][]) → String[]Examples
Example 1
operations = [["SERIALIZE","1(2,3(4,5))"],["DESERIALIZE","7:3|8:0|9:1|10:0|11:0"]]return = ["1:2|2:0|3:2|4:0|5:0","7(8,9(10),11)"]The first operation writes each node in preorder, next to its number of children. In the second operation, the root has three children. The middle child has one child of its own, so it is written with that child in parentheses.
Example 2
operations = [["SERIALIZE","42"],["DESERIALIZE","5:2|6:0|7:0"]]return = ["42:0","5(6,7)"]A leaf has child count 0. The second wire representation describes a root with two leaf children.
Example 3
operations = [["DESERIALIZE","1:1|2:1|3:1|4:0"],["SERIALIZE","0(10(20),30)"]]return = ["1(2(3(4)))","0:2|10:1|20:0|30:0"]The first wire string is a chain of four nodes, where every node except the last has exactly one child. In the second tree, the subtree under 10 comes before the leaf 30, and the wire format keeps that left-to-right order.
Constraints
1 ≤ operations.length ≤ 500.- Each operation is exactly
["SERIALIZE", treeText]or["DESERIALIZE", wire]. - Every tree has at least one node and at most
2000nodes, and the order of children matters. - Every node value is between
0and10^9, inclusive. - Every input string follows its format above exactly and has no spaces.
- Across all operations, the trees have at most
20000nodes in total.
More LinkedIn Onsite Interview questions
- Allocate Mailboxes with Squared DistanceONSITE INTERVIEW · Seen Nov 2026
- Find Leaves of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Max Points on a LineONSITE INTERVIEW · Seen Nov 2026
- Explore an Unknown Grid and Find a PathONSITE INTERVIEW · Seen Nov 2026
- Fire Station Response BillONSITE INTERVIEW · Seen Nov 2026
- Roll a Weighted DieONSITE INTERVIEW · Seen Oct 2026
- Longest Binary Subarray With at Most K OnesONSITE INTERVIEW · Seen Oct 2026
- Kth Closest Element in a BSTONSITE INTERVIEW · Seen Oct 2026
- Longest Binary Window with at Most K OnesONSITE INTERVIEW · Seen Oct 2026
- LRU Cache OperationsONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Serialize and Deserialize a Binary TreeAmazon · ONSITE INTERVIEW
- N-Ary Tree BFS CodecGoogle · ONSITE INTERVIEWGoogle interview questions
- Spreadsheet Formula DependenciesAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Serialize and Deserialize a Binary TreeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Serialize and Deserialize an N-Ary TreeUber · ONSITE INTERVIEWUber interview questions
- Spreadsheet With Dependent CellsCharacter.AI · PHONE SCREEN
- Colored Rooted TreeRubrik · ONSITE INTERVIEWRubrik interview questions
- JSON Schema Object ValidatorDocuSign · ONSITE INTERVIEWDocuSign interview questions
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions