FastPrepEvaluate an Arithmetic Expression
Problems/Evaluate an Arithmetic Expression

Evaluate an Arithmetic Expression

Akuna Capital logoAkuna Capital● MediumINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview asked for an eval-style function in Python 3. The candidate said they got as far as the four basic arithmetic operators with precedence, without parentheses. That was the part they finished, not a limit on the full original task. This exercise uses that part. We added limits on the numbers and the in-between results, a rule for spaces, the precision of true division, a promise that input is valid, the function signature, and a rule against using built-in evaluators.

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

Implement an arithmetic evaluator. You are given a valid, nonempty string expression containing integer literals and the binary operators +, -, * and /. Return its numeric value.

Use this grammar for the exercise:

  • Each literal is an integer from 0 through 100, written in ordinary decimal notation. A literal has no leading zero unless it is exactly 0.
  • The expression alternates between a literal and a binary operator, starting and ending with a literal. It contains no parentheses, unary signs, decimal literals, variables or function calls.
  • Zero or more ASCII space characters may appear between complete tokens and at either end. Spaces cannot split a literal. No other whitespace is present.

Multiplication and division have higher precedence than addition and subtraction. Operators at the same precedence level are evaluated from left to right. Division is ordinary true division: keep fractional values rather than truncating or rounding them to an integer. A result may be negative.

Every input satisfies the grammar and the bounds below. Each literal immediately after / is nonzero. You do not need to return an error value for invalid input.

Return a finite floating-point number. If exact is the mathematical result, your answer is accepted when abs(answer - exact) <= 10^-9 * max(1, abs(exact)).

Implement the parsing and evaluation yourself. Do not pass the whole expression to eval, exec or another expression-evaluation engine. Use fresh local state for each call.

Function

evaluateArithmetic(expression: String) → double

Examples

Example 1

expression = "3+2*2"return = 7.0

Multiplication comes first: 2*2 = 4. Then 3+4 = 7.

Example 2

expression = " 7 / 2 - 5 * 3 + 8 "return = -3.5

The spaces do not change the tokens. True division gives 7/2 = 3.5, and multiplication gives 5*3 = 15. The result is 3.5-15+8 = -3.5.

Example 3

expression = "8/4/2+6-3*2"return = 1.0

The first division chain is evaluated from left to right: (8/4)/2 = 1. Also, 3*2 = 6. The final value is 1+6-6 = 1.

Constraints

  • 1 <= expression.length <= 200.
  • The expression has between 1 and 10 integer literals, inclusive. Each is between 0 and 100, inclusive, with no leading zero except the single literal 0.
  • Only decimal digits, the four binary operators +, -, *, /, and ASCII space U+0020 occur. Spaces appear only between complete tokens or at the ends. The valid expression starts and ends with a literal.
  • Each literal immediately following / is between 1 and 100, inclusive.
  • Split the expression at binary + and - into signed multiplicative terms. Within each term, evaluate * and / from left to right. Every exact prefix value within a term, including its first literal, has absolute value at most 10,000. After each complete signed term is added from left to right, the exact cumulative sum also has absolute value at most 10,000.

More Akuna Capital Phone Screen questions

  • Match Orders by Price and Arrival TimeOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Biased Coin: Probability of Exactly h HeadsPHONE SCREEN · Seen Nov 2026
  • Positive Nth Root with Gradient DescentPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Two SumOA · PHONE SCREEN · Seen Sep 2026
  • Maximum Value in a Binary TreePHONE SCREEN · Seen Sep 2026
  • Tiered Expiring Item StorePHONE SCREEN · Seen Aug 2026
  • Statistics for the Latest K ValuesPHONE SCREEN · Seen Nov 2025
  • Remove Duplicates While Preserving OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2025
  • Sliding Window MaximumPHONE SCREEN · Seen Oct 2025
  • Evaluate a PolynomialPHONE SCREEN · Seen Oct 2025

Similar questions at other companies

  • Evaluate an Arithmetic Expression Written in WordsBrex · PHONE SCREEN
  • Evaluate a Flat Expression or Report Invalid InputMeta · PHONE SCREENMeta interview questions
  • Strict Text to Integer for a Given SizeAdobe · PHONE SCREENAdobe 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

Interview reports that mention this question

  • Akuna Capital QR Screens and Coding: 77 Experiences (Part 1 of 6)

Akuna Capital interview experiences

  • Akuna Capital Quant Trader and QR Interviews: 5 Experiences
  • Akuna Capital JQR CodePair Rounds: 8 Experiences (Part 6 of 6)
  • Akuna Capital QR Probability Phones: 14 Experiences (Part 5 of 6)
  • Akuna Capital QR-ML Interviews: 11 Experiences (Part 4 of 6)
  • Akuna Capital QR Regression Rounds: 11 Experiences (Part 3 of 6)
All Akuna Capital interview questionsSee Akuna Capital hiring insights
Saved
class Solution {
    public double evaluateArithmetic(String expression) {
        // write your code here
    }
}
Checking account…