Reported September 2026
Appletree

Canonical N-Ary Tree Codec

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

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

The mistake that sinks a first attempt on this Apple OA, reported in September 2026, is treating it like a plain string cleanup. It looks like a regex job. Strip spaces, trim zeros, done. Then "-2" and "0" and "-0" style numbers wreck your output. The task is an N-ary tree codec: parse the grammar node := integer '[' (node (',' node)*)? ']', then print it back canonically. You're really doing tree parsing with a length of up to 2 * 10^5, so recursion depth matters too. If you blank under the clock, StealthCoder is the safety net running invisibly during the live OA.

The problem

Deserialize the N-ary tree encoded by data, then serialize it into canonical form.
The grammar is node := integer '[' (node (',' node)*)? ']'. Input may contain whitespace and leading zeroes. Canonical output contains no whitespace or leading zeroes.

Function
canonicalizeNaryTree(data: String) → String

Examples
Example 1
data = "1[3[5[],6[]],2[],4[]]"
return = "1[3[5[],6[]],2[],4[]]"
The canonical input is preserved after a codec round trip.
Example 2
data = " 01[ -2[], 003[] ] "
return = "1[-2[],3[]]"
Whitespace and leading zeroes are normalized.
Example 3
data = "-7[]"
return = "-7[]"
A leaf has an empty child list.

Constraints
1 <= data.length <= 2 * 10^5.
The input is a valid tree in the stated grammar.
Node values fit signed 32-bit integers.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that you don't need to build a tree at all. A single left-to-right pass with a stack, or a recursive descent with an index pointer, does the parse and the print together. Skip whitespace, read an optional minus sign, read digits, convert to an integer, then emit its canonical form. Converting to an int is what kills leading zeroes, and "-0" style input collapses to 0 naturally. Then handle '[', and loop on ',' until ']'. The common pitfall is recursion depth. With 2 * 10^5 characters, a deeply nested chain can overflow the call stack in many languages, so go iterative with an explicit stack. The second pitfall is string concatenation in a loop, which goes quadratic. Append to a list and join once. StealthCoder is your hedge if the iterative stack version won't come to you live in the OA.

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 Canonical N-Ary Tree 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 Apple's OA.

Apple 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.

Canonical N-Ary Tree Codec FAQ

What's the trick in the Apple canonical N-ary tree codec problem?+

Parse and print in one pass. Skip whitespace, read a signed integer, convert it to a real int so leading zeroes vanish, then emit it with brackets and commas. You never need to store an actual tree unless you want the clarity of it.

Will recursion work for this problem?+

Only if the language handles depth well. Input can be 2 * 10^5 characters, so a deeply nested chain can blow the stack. Safer to use an explicit stack and iterate. If you do go recursive, know your language's limits before you submit.

How do I handle negative numbers and leading zeroes?+

Read an optional '-' first, then consume digits, then parse the whole token as an integer and print it back. That turns "003" into 3 and "-2" stays -2. Values fit signed 32-bit, so a normal int parse is fine.

How hard is this one really?+

Medium. The grammar is simple, but the size and the whitespace edge cases trip people up. It's a parsing problem dressed up as a tree problem. If you've written a bracket parser before, it's fast.

How do I prepare for this in 48 hours?+

Write a stack-based bracket parser from scratch twice. Test inputs with extra spaces, a leaf like "-7[]", and a very deep chain. Check your output building is linear, using a list join instead of repeated string appends.

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

OA at Apple?
Invisible during screen share
Get it