Reported September 2026
Googlebreadth first search

N-Ary Tree BFS Codec

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

The sample in this Google problem, 1(3(5,6),2,4) turning into 1|3,2,4,#|5,6,#,#,#, tells you what the whole question is about. Candidates reported it in September 2026. It's an N-ary tree codec with two directions: parse canonical text into a tree, then write it level by level with # closing each parent's child list. It's a breadth-first search problem wearing a parsing costume. If the OA is in a day or two, the work is knowing both directions cold. StealthCoder sits invisibly on screen as a safety net if you blank mid-assessment.

The problem

Implement both directions of a codec for ordered N-ary trees with signed integer node values. Process each row of operations in order and return one result per row.
["SERIALIZE", tree] converts canonical tree text into the breadth-first wire format below.
["DESERIALIZE", data] converts valid wire data into canonical tree text.
Canonical tree text
An empty tree is the empty string. A leaf is its decimal value. A non-leaf is its value followed by its children in parentheses, separated by commas. Child order is significant. For example, 1(3(5,6),2,4) has root 1 with children 3, 2, and 4.
Breadth-first wire format
An empty tree is again the empty string. A nonempty encoding begins with the root value. Later levels are separated by |. Within a later level, list each parent's child values from left to right and terminate that parent's child list with #. Parents are processed in the previous level's left-to-right order. Omit a final level that would contain only # markers.
For example, 1(3(5,6),2,4) serializes as 1|3,2,4,#|5,6,#,#,#. In the last segment, the first # closes node 3's children, and the remaining markers record 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 wire string gives the root two leaf children.

Constraints
1 <= operations.length <= 200.
Every operation has exactly two strings and uses SERIALIZE or DESERIALIZE.
Every supplied tree text and wire string is valid under the stated grammar.
Each tree contains at most 5000 nodes.
Every node value is a signed 32-bit integer.
The total number of nodes across all operations is at most 20000.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that you do two jobs per operation. SERIALIZE needs a recursive-descent or stack parser for the canonical text, including negative values and nested parentheses, then a BFS queue. For each node in the queue, append its children and then a #, and join levels with |. Drop the final level if it's only # markers. DESERIALIZE reverses it: keep a queue of parents for the previous level, read the next segment, and consume children until each # closes a parent. The pitfalls are trailing all-# levels, the single-node case, and splitting on commas without respecting nesting. With 5000 nodes per tree, avoid deep recursion if your language has a small stack. Use an explicit stack for parsing. If you blank on the pointer logic during the live OA, StealthCoder can give you the structure so you can verify against the examples.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill N-Ary Tree BFS Codec cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

N-Ary Tree BFS Codec FAQ

How hard is the N-Ary Tree BFS Codec really?+

Medium to medium-hard. The BFS idea is easy. The work is the text parser and the exact # rules. Expect most of your time on edge cases like trailing # levels, negative values, and the empty string, not on the traversal.

What's the trick to the wire format?+

Each parent in a level gets exactly one # after its children, even if it has none. So the count of # in a segment equals the number of parents in the previous level. Use that count to know when a segment is fully consumed.

How do I parse the canonical tree text safely?+

Use an explicit stack. Read an integer including an optional minus sign, create a node, attach it to the stack top. An open paren pushes the current node, a close paren pops. Commas just move to the next sibling. Don't split on commas.

Which edge cases should I test first?+

Test the empty string both ways, a single node like 42, a negative root like -1(2,3), and a tree whose last level has no children. That last one checks that you omit a final all-# level, as the statement requires.

How do I prepare in 48 hours?+

Write both functions from scratch once and run Example 1 and Example 2 by hand. Practice a BFS with a queue and a level-by-level loop. Then round-trip a few random trees: serialize, deserialize, and compare to the original text.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it