Design an Expression Evaluator
🧮 What is an expression evaluator? It is the piece of code behind a calculator, a spreadsheet formula bar or a pricing rule like
base * 2 + fee. It takes a string, works out which operations to apply in which order, and returns a number. The hard part is not the math; it is reading the string correctly.
The same question also comes up as "design an arithmetic operations engine": a system that knows a set of operations (add, multiply, power...) and applies them to inputs. That is the same design seen from the other end. The engine is our operator table, where each operator is bound to one operation, and the evaluator is the part that reads text and decides which operation runs on which values. We cover both here.
Interviewers like this problem because it has a clear right answer for correctness (2 + 3 * 4 is 14, not 20) and a clear design question on top of it: when the business wants a new operator, how much of your code has to change?
Requirements
"Design an expression evaluator. Given a string like
2 + 3 * 4, return the result. We will want to add new operators later."
That leaves a lot open: what can appear in the string, what kind of numbers, and what "add new operators" means. The questions below settle the rules we code against.
Clarifying Questions
You: What can appear in an expression?
Interviewer: Whole numbers, the four operators + - * /, parentheses and spaces. Also variable names, like price * qty, whose values are set beforehand.
You: What kind of numbers: integers or decimals? And how does division round?
Interviewer: 64-bit integers. Division truncates toward zero, like Java and C++: 7 / 2 is 3 and -7 / 2 is -3.
You: Do we need a minus sign in front of a value, like -x or 2 * -3?
Interviewer: Yes, unary minus. No unary plus.
You: What does "add an operator" look like? Can users write their own code for it?
Interviewer: No code. An admin picks a one-character symbol, a precedence, whether it groups left or right, and one of a fixed list of operations: add, subtract, multiply, divide, modulo, power, max and min. For example, ^ as power with the highest precedence, grouping right.
You: What happens on bad input: a syntax error, division by zero, a variable that was never set?
Interviewer: Return the string "ERROR". We don't need error messages for now.
You: Will the same expression be evaluated many times with different variable values?
Interviewer: Maybe later. Keep it in mind.
The fourth answer shapes the whole design. "New operators at runtime, from data" means the parser cannot have the operators written into its code. Precedence and associativity have to be values the parser reads, not functions it calls.
💡 Interview tip: Ask how
-2 ^ 2should evaluate. There is no universal answer: math textbooks say -4, many programming languages say 4. Whatever your interviewer picks, write it down as a rule, because it decides where unary minus sits in your parser.
Here the interviewer chose: unary minus binds tighter than every binary operator, so -2 ^ 2 is (-2) ^ 2 = 4.
Final Requirements
- An expression contains non-negative integer literals, variable names (a letter followed by letters or digits), binary operators, unary
-, parentheses and spaces. - The evaluator starts with
+and-(precedence 1) and*and/(precedence 2), all grouping left. A higher precedence binds tighter. - Unary
-binds tighter than every binary operator. There is no unary+. - Values are 64-bit integers. Division truncates toward zero; modulo is the matching remainder, with the sign of the left side. A negative exponent is an error.
- Variables can be set (and replaced) by name. Using a variable that was never set is an error.
- A new binary operator can be added at runtime: a one-character symbol, a precedence, an associativity, and one of eight named operations. A symbol that is a letter, digit, space, parenthesis or an existing operator is refused.
- Any error (bad syntax, unknown operator, division by zero, negative exponent, unknown variable) makes
evaluatereturn"ERROR".
Out of scope
- Decimals, big numbers and overflow detection (inputs are chosen so values fit in 64 bits).
- Function calls like
max(a, b)(we come back to this in Extensibility). - Error messages with positions.
- User-written operator code.