Reported July 2026
Optivertree

Construct Binary Tree S-Expression

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

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

The edge case that breaks a naive solution on this Optiver OA, reported July 2026, is error priority. You parse the pairs, build the tree, print it, and then a test hands you a duplicate pair that also makes a parent with three children. Which code do you return? This is a tree problem wearing a string-parsing costume. You validate the input, count edges, find roots, detect cycles, then serialize recursively with sorted children. If you blank on the ordering of checks, StealthCoder is the safety net running invisibly during the live OA.

The problem

You are given a string containing parent-child pairs for a binary tree. Each pair is formatted like (A,B), meaning A is the parent of B.
If the pairs form a valid binary tree, return its S-expression. A node is rendered as (value leftSubtree rightSubtree), omitting empty children. When a node has two children, render them in lexicographic order by node label.
If the input is invalid, return the highest-priority error code:
E1: Invalid input string.
E2: Duplicate pair.
E3: A parent has more than two children.
E4: Multiple roots.
E5: Cycle in the tree.

Function
validateBinaryTree(pairs: String) → String
Complete validateBinaryTree.
String pairs: a space-separated list of parent-child pairs
Returns String: either an S-expression or an error code.

Examples
Example 1
pairs = "(A,B) (B,C) (A,D)"
return = "(A(B(C))(D))"
The pairs form a valid tree rooted at A.
Example 2
pairs = "(A,B) (A,B)"
return = "E2"
The pair (A,B) appears twice.

Constraints
1 <= pairs.length <= 10^5
Node labels are uppercase English letters in the visible examples; hidden tests may use the same token format.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is running validation in strict priority order, because the spec says to return the highest-priority error. Check E1 first: every token must match (X,Y) exactly. Then E2 with a set of seen pairs. Then E3 by counting children per parent. Then E4 by collecting nodes that never appear as a child, and more than one means multiple roots. Then E5 for cycles. The pitfall is that a pure cycle like (A,B) (B,A) has zero roots, so decide how you handle that and make sure it lands on E5, not a crash. Also don't recurse blindly on 10^5 nodes without thinking about depth. Once valid, build a child map, sort each node's children, and emit (value left right) recursively. If the order of checks or the cycle case slips away mid-assessment, StealthCoder can hand you a working structure live.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Construct Binary Tree S-Expression 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Optiver reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Construct Binary Tree S-Expression FAQ

What's the trick in the Optiver Construct Binary Tree S-Expression problem?+

Validate in priority order: E1 format, E2 duplicates, E3 more than two children, E4 multiple roots, E5 cycle. Only after all pass do you build the S-expression. Most failures come from returning a lower-priority error when a higher one also applies.

How do I detect a cycle here?+

With a valid single root, DFS from it and count visited nodes. If the count is less than the total number of distinct nodes, something is unreachable, which means a cycle. A graph with no root at all is also a cycle case, so handle it explicitly.

How do I handle the lexicographic ordering of children?+

Store children per parent in a list, and sort it when a parent has two. Since labels are single uppercase letters in the examples, plain string comparison works. Render the smaller label first as the left subtree, then the larger as the right.

Is this pattern still asked as of July 2026?+

It was reported for Optiver in July 2026, so treat it as current. Tree construction with validation and error codes shows up often because it tests careful edge-case handling more than clever algorithms.

How do I prepare for this in 48 hours?+

Write the parser and the five checks as separate small functions, then test with tiny inputs that trigger two errors at once. Practice a recursive serializer on a hand-built tree. Focus on malformed tokens, duplicates, and cycles, since those cases decide pass or fail.

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

OA at Optiver?
Invisible during screen share
Get it