FastPrepEvaluate an Arithmetic Expression Written in Words
Problems/Evaluate an Arithmetic Expression Written in Words

Evaluate an Arithmetic Expression Written in Words

Brex logoBrex● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Brex asked this in a technical phone screen. The candidate had to work out the value of a math expression spelled with words: numbers, plus, times, and a negative sign word, with no parentheses. Writing their own test cases was part of the task.

The report gives one example but no limits, and it does not spell out the order of operations. Our choices here: times goes before plus as in normal math, and negative only flips the number right after it. Words and numbers are split by single spaces with none at either end, and the expression is always valid and 1 to 10,000 characters long. Numbers are whole numbers from 0 to 10,000 written in digits with no leading zeros. Every value along the way stays within 10^15, and the answer comes back as a 64-bit integer.

Our version is about 85% the same as the reported question.

An arithmetic expression is written with words instead of symbols, for example "5 plus 4 times negative 5". Evaluate expression and return its value.

The expression is a list of tokens separated by single spaces:

  • A number is a non-negative integer written in digits, such as 0 or 42.
  • The word negative can appear right before a number. It makes that one number negative.
  • The word plus means addition and the word times means multiplication.

Multiplication comes before addition, as in ordinary math. First multiply each run of numbers joined by times into one term, then add all the terms. The expression has no parentheses and is always valid.

Function

evaluateExpression(expression: String) → long

Examples

Example 1

expression = "5 plus 4 times negative 5"return = -15
  • times goes first: 4 times negative 5 is 4 × (-5) = -20.
  • Then add: 5 + (-20) = -15.

Example 2

expression = "negative 3 times 2 times negative 4 plus 10"return = 34
  • The first term multiplies three numbers: (-3) × 2 × (-4) = 24.
  • Then add the second term: 24 + 10 = 34.

Example 3

expression = "12 times 0 plus negative 8"return = -8
  • 12 × 0 = 0.
  • Then 0 + (-8) = -8.

Constraints

  • 1 <= expression.length <= 10^4
  • expression is made of tokens separated by single spaces, with no space at the start or the end.
  • An operand is a number, or the word negative, a space and a number. The expression starts and ends with an operand, and operands and the operators plus and times take turns.
  • Every number is an integer from 0 to 10^4 written in digits with no leading zeros.
  • While you evaluate as described, every partial product, every term, and every running total of the terms from the left has an absolute value of at most 10^15.

More Brex Phone Screen questions

  • Task Scheduler with Prerequisites and Execution PathsPHONE SCREEN · Seen Nov 2026
  • Concurrent Discounted Card PurchasesPHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Evaluate a Flat Expression or Report Invalid InputMeta · PHONE SCREENMeta interview questions
  • Strict Text to Integer for a Given SizeAdobe · PHONE SCREENAdobe interview questions
  • Evaluate an Arithmetic ExpressionAkuna Capital · PHONE SCREENAkuna Capital interview questions
  • Solve a Linear Equation in xNutanix · PHONE SCREENNutanix interview questions
  • Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
  • Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesStripe · PHONE SCREENStripe interview questions
  • Parse, Normalize, and Combine PolynomialsSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
  • Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
  • Simplify a Quadratic ExpressionOpenAI · OAOpenAI interview questions
  • Balance a Chemical EquationMicrosoft · OAMicrosoft interview questions

Brex interview experiences

  • Brex SWE Phone Screen: Task Scheduler: 1 Experience
See Brex hiring insights
Saved
class Solution {
    public long evaluateExpression(String expression) {
        // Write your code here.
    }
}
Checking account…