Design an Expression Tree From Postfix Tokens With an Evaluate Method
Company: Smartsheet
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Given the postfix tokens of an arithmetic expression, build a binary expression tree for the expression and return the value it evaluates to.
Each internal node of the tree holds one of the operators `+`, `-`, `*`, `/`, and each leaf holds an integer operand. Operands are non-negative integers. All intermediate results are integers, and division truncates toward zero.
Implement the `TreeBuilder` class. The nodes it builds are polymorphic: every node exposes a single `evaluate()` method, and what that method does depends on whether the node is an operand or an operator.
- `buildTree(postfix: List[str]) -> int` builds the expression tree from the postfix tokens and returns the integer value of its root, the same value you get by calling `evaluate()` on the root node.
Example 1:
```text
builder = TreeBuilder()
builder.buildTree(["3", "4", "+", "2", "*", "7", "/"]) # returns 2
```
The tree computes `((3 + 4) * 2) / 7`, which is `14 / 7 = 2`.
Example 2:
```text
builder = TreeBuilder()
builder.buildTree(["4", "5", "2", "7", "+", "-", "*"]) # returns -16
```
The tree computes `4 * (5 - (2 + 7))`, which is `4 * -4 = -16`.
```hint Where the subtrees come from
In postfix order, an operator always applies to the two most recently completed subexpressions. Pick a structure that hands those back to you in that order.
```
```hint Operand order and division
For `-` and `/` the order of the two children matters. Also check what your language's integer division does when the result is negative.
```
### Constraints & Assumptions
- `1 <= postfix.length <= 100`
- Each token is an operator from `{+, -, *, /}` or a non-negative integer written as a string.
- The postfix expression is always valid.
- The answer and every intermediate value fit in a 32-bit signed integer.
- Division by zero never occurs, and integer division truncates toward zero.
### Clarifying Questions to Ask
- Should callers also be able to get the root node itself, for example to evaluate it again or print it, or is the integer result all they need?
- Are new operators likely to be added later, and should adding one avoid edits to the existing node classes?
- If the input were malformed, should the builder raise an error, or can I rely on the validity guarantee?
### What a Strong Answer Covers
- A node abstraction with a single `evaluate()` method and separate leaf and operator implementations, so evaluation never branches on node type.
- Building the tree in one pass over the tokens, with the left and right children assigned in the correct order.
- Division that truncates toward zero for negative intermediate results in the chosen language.
- The time and space cost of building and evaluating, including recursion depth.
- How the design extends to a new operator or a new kind of node.
### Follow-up Questions
- How would you add `%` or a unary negation operator without modifying the existing node classes?
- How would you build the same tree from an infix string with parentheses and operator precedence?
- How would you print the tree back as an infix string that uses only the parentheses it needs?
- What would you change if an expression could have hundreds of thousands of tokens?
Overview: Build a binary expression tree from postfix tokens using polymorphic operand and operator nodes, then evaluate it with integer division that truncates toward zero. Tests stack-based tree construction, object-oriented node design and the operand-order and negative-division pitfalls.