Evaluate a Variadic Addition or Subtraction Expression
Company: Anduril
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
# Evaluate a Variadic Addition or Subtraction Expression
Implement an evaluator for an array whose first element is an operator and whose remaining elements are integer operands.
- `"+"` returns the sum of every operand.
- `"-"` starts with the first operand and subtracts each later operand from left to right.
For example:
```text
["+", 1, 2, 3] -> 6
["-", 10, 5, 3] -> 2
```
Return the computed integer.
### Constraints & Assumptions
- The expression contains a supported operator followed by at least one integer.
- Do not reorder operands; subtraction is left-associative.
- The core implementation only needs to support addition and subtraction.
### Clarifying Questions to Ask
- Should a one-operand subtraction expression return that operand or its negation?
- How should an unsupported operator be reported?
- If multiplication and division are added, should this array remain a single variadic operation or support nested expressions and precedence?
```hint Identify the accumulator seed
Addition can start from its identity, but left-associative subtraction needs the first operand as its initial accumulator.
```
```hint Separate dispatch from iteration
Choose the operation once, then process the operands without checking the operator on every branch of the loop.
```
### Evaluation Criteria
- Correct handling of variadic addition and left-associative subtraction.
- A clear empty or malformed-input policy instead of an accidental index error.
- A design that can add operators without duplicating the traversal logic.
- Linear time in the number of operands and constant auxiliary space.
### Extensions to Discuss
- Where would you add multiplication and division support?
- What division-by-zero and integer-versus-floating-point rules would the extended evaluator need?
- How would the representation change if expressions could be nested?
Quick Answer: Implement a variadic expression evaluator that supports addition and left-associative subtraction over integer operands. Practice accumulator initialization, malformed-input policies, linear-time traversal, and an extensible operator-dispatch design.
Implement evaluate_expression(operator, operands). The operator is either "+" or "-", and operands is a nonempty list of integers. Addition returns the sum of all operands. Subtraction starts with the first operand and subtracts each later operand from left to right.
Constraints
- operator is exactly "+" or "-".
- 1 <= operands.length <= 20.
- Each operand is an integer from -2,147,483,648 through 2,147,483,648.
- The mathematical result is within JavaScript's exact integer range, from -(2^53 - 1) through 2^53 - 1.
- Subtraction is evaluated from left to right without reordering operands.
Examples
Input: ('+', [5])
Expected Output: 5
Explanation: Addition with one operand returns that operand.
Input: ('-', [5])
Expected Output: 5
Explanation: One-operand subtraction uses the first operand as the accumulator.
Hints
- Addition has an identity value, but subtraction needs the first operand as its initial accumulator.
- Select the operator behavior once, then make one linear pass over the relevant operands.