FastPrepConstruct Binary Tree S-Expression
Problems/Construct Binary Tree S-Expression

Construct Binary Tree S-Expression

Optiver logoOptiver● MediumOA

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This came from a real Optiver OA report. The original report shared the pair input, valid S-expression output, and error priority list; FastPrep filled in the runnable function format and small practice examples so you can try it here. Estimated match rate: 90-95% for the core task, 70-80% for exact wording and runner details. If you find a fuller version, feel free to let us know and we will update it.

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:

  1. E1: Invalid input string.
  2. E2: Duplicate pair.
  3. E3: A parent has more than two children.
  4. E4: Multiple roots.
  5. 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.

More Optiver Online Assessment questions

  • Chance a Die Game Balance Reaches ZeroOA · Seen Dec 2026
  • Chance a Hand of Cards Multiplies to More Than a ThresholdOA · Seen Dec 2026
  • Chance a Sum of Random Draws Has a Given Number of DigitsOA · Seen Dec 2026
  • Expected Draws to Remove All Green CountersOA · Seen Dec 2026
  • Expected Profit of a Doubling Betting SystemOA · Seen Dec 2026
  • Expected Steps Until a Random Walk Gets Far From ZeroOA · Seen Dec 2026
  • Rolling Until One Face's Own Total Passes a ThresholdOA · Seen Dec 2026
  • Two Ranked Teams Meeting in a Random BracketOA · Seen Dec 2026
  • Overheat Prevention ControllerOA · Seen Nov 2026
  • Power Cell BankOA · Seen Nov 2026

Similar questions at other companies

  • Count Parent Nodes With a Target SumAdobe · OAAdobe interview questions
  • Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
  • Match a Descendant Selector in a DOM TreeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • N-Ary Tree Codec OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Recover a Tree from Preorder Depth EncodingSuperhuman · PHONE SCREENSuperhuman interview questions
  • Count Prime Values in SubtreesVisa · OAVisa interview questions
  • Part 1: The Narrowest KeyScale AI · OAScale AI interview questions
  • Sewer Drainage PartitionTwo Sigma · OATwo Sigma interview questions
  • Merge Row Trees by Key PathHudson River Trading · OAHudson River Trading interview questions
  • Count Balanced Nodes in a Rooted TreeDRW · OADRW interview questions

Optiver interview experiences

  • Optiver SWE Intern OA: CPU Temperature Simulation: 1 Experience
  • Optiver Trading Intern Market-Making Games: 1 Experience
  • Optiver Quant Trader Intern Technical Interview: 1 Experience
  • Optiver SWE Intern System Design Interview: 15 Experiences
All Optiver interview questionsSee Optiver hiring insights
Saved
class Solution {
    public String validateBinaryTree(String pairs) {
      // write your code here
    }
}
Checking account…