What Determines Evaluation Order in a Prefix (Polish Notation) Expression?
Company: Verisk
Role: Data Scientist
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Online Assessment
An online assessment includes this multiple-choice question.
Which of the following factors determine the order of evaluation of a prefix expression?
- **A.** Precedence of operators
- **B.** Associativity of operators
- **C.** Both precedence and associativity
- **D.** Neither precedence nor associativity
Choose one option and justify it with a small example. In a prefix expression (Polish notation), each operator is written before its operands, so the infix expression `2 + 3` is written `+ 2 3`.
```hint Draw the trees
Write `* + 2 3 4` and `+ 2 * 3 4` as expression trees, and notice what you relied on to group each one.
```
```hint Where the rules come from
Ask why `2 + 3 * 4` needs a rule before anyone can evaluate it, and whether a prefix string can leave the same question open.
```
### Clarifying Questions
- Does every operator take a fixed number of operands, for example is `-` always binary?
- Does "order of evaluation" mean which operator applies to which operands, or the order in which an implementation happens to compute independent subexpressions?
- May the expression be assumed to be well formed?
### What a Strong Answer Covers
- The correct option, with the reason a prefix expression has only one possible grouping
- What precedence and associativity do in infix notation, with an example of each
- The assumption that makes prefix notation unambiguous, and what breaks without it
- How a prefix expression is evaluated in a single pass, including the operand order for non-commutative operators
### Follow-up Questions
- Evaluate `- * 3 4 / 8 2` and describe the procedure you used.
- Convert the infix expressions `a - b - c` and `2 ^ 3 ^ 2` (with `^` as exponentiation) to prefix. Where do precedence and associativity come back into play?
- If `-` may be either unary negation or binary subtraction, what values can `- - 5 3` have?
Overview: A multiple-choice question asks whether operator precedence, associativity, both or neither determine the order of evaluation of a prefix (Polish notation) expression. It tests why infix notation needs those rules, how prefix expressions are parsed and evaluated, and why fixed operator arity matters.
Read the full Verisk Data Scientist interview experience this question came from