Reported September 2026
Amazontree

Simplify a Binary Expression Tree

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

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

The mistake that sinks a first attempt on this Amazon OA, reported in September 2026, is simplifying the parent before the children are done. The task: parse a prefix-serialized binary expression tree, simplify it bottom-up, and print one space-separated prefix string. Folding integers is easy. The identity rules (x + 0, x * 1, x * 0) are where people lose points. It's a tree recursion problem in disguise, with a parsing step on top. If you blank during the live assessment, StealthCoder runs invisibly on your desktop as a safety net and gives you the working structure.

The problem

A valid binary expression tree is serialized in prefix order. Each token is +, -, *, a variable name, or a signed integer.
Simplify the tree bottom-up. Fold an operator when both children are integers. Also apply x + 0 = x, 0 + x = x, x - 0 = x, x * 1 = x, 1 * x = x, and x * 0 = 0. Return the simplified tree as one space-separated prefix expression.

Function
simplifyExpressionTree(preorder: String[]) → String

Examples
Example 1
preorder = ["+","*","x","1","0"]
return = "x"
Multiplication by one and addition of zero both disappear.
Example 2
preorder = ["*","+","2","3","y"]
return = "* 5 y"
The constant addition folds while the variable multiplication remains.

Constraints
1 <= preorder.length <= 10000.
The prefix sequence is a valid binary expression tree.
Every folded result fits in a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a single recursive function that consumes tokens from a shared index. Read a token. If it's a variable or integer, return it as a leaf. If it's an operator, recurse for the left child, then the right, and only then simplify. Return either a string for a leaf or a small node object, so you can tell whether a child is an integer. Check order matters: fold two integers first, then apply identities. The pitfall is x * 0 = 0 when x is a whole subtree, since the result must be the integer 0 and that may enable another fold higher up. Also watch subtraction: 0 - x is NOT simplified by the rules given. Negative integers like -5 are tokens, not operators, so a lone - only means subtraction when it's in operator position. With 10000 tokens, recursion depth can hurt in some languages, so consider an explicit stack. StealthCoder is your hedge if the recursion setup slips mid-assessment.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Simplify a Binary Expression Tree 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Simplify a Binary Expression Tree FAQ

What's the trick to the Amazon expression tree simplification problem?+

Recurse in preorder with a shared index. Build and simplify children first, then apply the parent's rule. Return a node that knows whether it's an integer constant. Folding and identity checks happen only after both children are final, which is what makes bottom-up simplification correct.

How do I tell a negative number from the minus operator?+

Position and length. The token "-" by itself is the operator. A token like "-5" is a signed integer leaf. Since the input is a valid prefix tree, you never have to guess from context. Just compare the whole token string, don't check the first character alone.

Is 0 - x or x * 0 handled the same way as the other rules?+

No. The listed rules only cover x - 0, not 0 - x, so leave 0 - x alone. But x * 0 returns 0 even when x is a big subtree, and that new integer 0 can trigger folds or identities in the parent. Don't forget the symmetric case 0 * x.

Will deep recursion break on 10000 tokens?+

It can. A skewed tree gives depth near 5000 in some cases. Python's default limit will bite, so raise it or use an explicit stack processing tokens in reverse. In Java or C++ it's usually fine, but know your language's limit before submitting.

How do I prepare for this in 48 hours?+

Practice parsing a prefix expression recursively, then add one rule at a time: constant folding, then identities. Test examples with nested folds and a multiplication by zero that cascades upward. Write the output by joining tokens in preorder. That covers nearly every failure mode here.

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

OA at Amazon?
Invisible during screen share
Get it