Serialize and Deserialize an N-Ary Tree
Updated
Problem statement
Implement a codec for finite rooted trees with integer values. Each node may have any number of children, and their order matters. Process each operation independently and return one result per operation.
- For
serialize, the payload is a canonical tree description: a node isvalue[child1,child2,...], a leaf isvalue[], and an empty tree is#. Return the preorder child-count encoding of that tree: visit each node before its children. Process children in order, visiting each child’s entire subtree before moving to the next child. For each non-null node, write its value followed by its number of children. Separate tokens with one space. The empty tree encodes as#. - For
deserialize, the payload is a preorder child-count encoding produced by that rule. Rebuild the tree and return its canonical tree description.
Child order must be preserved exactly.
Function
processNaryCodec(operations: String[], payloads: String[]) → String[]Examples
Example 1
operations = ["serialize","deserialize"]payloads = ["1[2[],3[4[]]]","1 2 2 0 3 1 4 0"]return = ["1 2 2 0 3 1 4 0","1[2[],3[4[]]]"]The first operation encodes the ordered tree in preorder. The second reconstructs the same tree and emits its canonical description.
Example 2
operations = ["serialize","deserialize"]payloads = ["#","-5 0"]return = ["#","-5[]"]The null tree has the single-token encoding #, while a leaf stores its value and a zero child count.
Constraints
1 <= operations.length == payloads.length <= 100- Every operation is
serializeordeserialize, and each payload is valid for its operation. - Across all operations there are at most
10000nodes and at most100000payload characters. - Node values are signed 32-bit integers.
More Uber Onsite Interview questions
- Earliest Time All Users Are ConnectedPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Online Meeting Scheduler with Fixed RoomsONSITE INTERVIEW · Seen Oct 2026
- Minimum Meeting Rooms with AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Longest Continuous Subarray Within an Absolute-Difference LimitONSITE INTERVIEW · Seen Oct 2026
- K Smallest Squares from a Sorted ArrayONSITE INTERVIEW · Seen Sep 2026
- Grid Robot Route with Charging PrioritiesPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Shortest Increasing Path to a TargetONSITE INTERVIEW · Seen Sep 2026
- My Calendar IONSITE INTERVIEW · Seen Sep 2026
- Per-Element Hit CounterONSITE INTERVIEW · Seen Sep 2026
- Cheapest Round TripONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Serialize and Deserialize a Binary TreeAmazon · ONSITE INTERVIEW
- N-Ary Tree Codec OperationsLinkedIn · ONSITE INTERVIEW
- Serialize and Deserialize a Binary TreeMicrosoft · ONSITE INTERVIEW
- File System Shell CommandsHarvey · ONSITE INTERVIEW
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEW
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW
- JSON Path Lookup with Implicit Array WildcardsMongoDB · ONSITE INTERVIEW
- Serialize and Deserialize a Binary TreeGoogle · ONSITE INTERVIEW
- Filesystem Hash TreeCursor · PHONE SCREEN · ONSITE INTERVIEW
- Nested Todo ListNotion · PHONE SCREEN · ONSITE INTERVIEW