FastPrepEvaluate an Arithmetic Expression with Division
Problems/Evaluate an Arithmetic Expression with Division

Evaluate an Arithmetic Expression with Division

Motive logoMotive● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand Motive report for a full-time onsite interview asks for expression evaluation. It calls out negative numbers and division by zero. We chose the operators, the parentheses, how the result is returned, and the number limits.

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

Given an arithmetic expression containing integers, spaces, parentheses, and the operators +, -, *, and /, evaluate it using ordinary precedence.

Unary + and - are allowed before a number or parenthesized expression. Integer division truncates toward zero. Return the decimal result as a string, or "DIVISION_BY_ZERO" if any evaluated division has a zero divisor.

Function

evaluateExpression(expression: String) → String

Examples

Example 1

expression = "2 + 3 * (4 - 1)"return = "11"

Multiplication and the parenthesized subtraction are evaluated before addition.

Example 2

expression = "-7 / 3"return = "-2"

Integer division truncates toward zero.

Example 3

expression = "5 / (3 - 3)"return = "DIVISION_BY_ZERO"

The evaluated divisor is zero.

Constraints

  • 1 <= expression.length <= 100000.
  • The expression is otherwise syntactically valid and every intermediate non-error result fits a signed 64-bit integer.

More Motive Onsite Interview questions

  • Group AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
  • Longest Equal-Zero-One SubarrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
  • Maximum Weighted Sum After Left RotationsONSITE INTERVIEW · Seen Jan 2026
  • Valid Parenthesis StringPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
  • Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Dec 2025
  • Target Triplet SumONSITE INTERVIEW · Seen Dec 2025
  • Validate IP AddressONSITE INTERVIEW · Seen Dec 2025
  • Generate Palindromic PermutationsONSITE INTERVIEW · Seen Dec 2025
  • Minimum Swaps to Group Circular OnesONSITE INTERVIEW · Seen Dec 2025
  • Staircase Search In A Sorted MatrixONSITE INTERVIEW · Seen Dec 2025

Similar questions at other companies

  • Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
  • Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Arithmetic Expression EvaluatorMicrosoft · PHONE SCREENMicrosoft interview questions
  • Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
  • Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
  • Flatten JSON With Array IndicesAdobe · PHONE SCREENAdobe interview questions
  • Decode StringSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
  • Plus and Minus CalculatorIndeed · PHONE SCREENIndeed interview questions
  • Parse Boolean Rule ExpressionsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
  • Basic CalculatorAmazon · PHONE SCREEN · ONSITE INTERVIEW
All Motive interview questionsSee Motive hiring insights
Saved
class Solution {
    public String evaluateExpression(String expression) {
        // Evaluate the expression or report division by zero.
    }
}
Checking account…