Evaluate a Flat Expression or Report Invalid Input
Updated
Problem statement
Evaluate a flat arithmetic expression with nonnegative decimal integer operands and binary operators +, -, *, and /. Multiplication and division take precedence over addition and subtraction; operators at the same precedence are evaluated left to right. There are no parentheses or unary signs.
ASCII spaces may appear before, after, or between complete tokens, but not inside a number. Leading zeros are allowed. Divide integers by truncating toward zero. Return the decimal result as a string.
Return the exact string ERROR if the expression is empty or malformed, contains an unsupported character, has an operand larger than 10^6, or divides by zero. Examples of malformed syntax include adjacent operators, a missing operand, an unexpected parenthesis, and two numbers with no operator between them.
Function
evaluateFlatExpression(expression: String) → StringExamples
Example 1
expression = "10/2+1*3+5"return = "13"Evaluate division and multiplication first: 5 + 3 + 5 = 13.
Example 2
expression = "20-7/2*3"return = "11"7/2 truncates to 3, then 3*3 = 9, so the result is 11.
Example 3
expression = "1+*2"return = "ERROR"The addition operator lacks a number on its right, so the syntax is invalid.
Constraints
0 <= expression.length <= 100.- Valid operands are decimal integers from
0through10^6. - For valid arithmetic, every intermediate value and the final result fit a signed 64-bit integer.
- Only ASCII spaces are allowed as whitespace.
More Meta Phone Screen questions
- Convert a Sorted Linked List to a Balanced BSTPHONE SCREEN · Seen Nov 2026
- Fuzzy Word Lookup With One Misspelled LetterPHONE SCREEN · Seen Nov 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Nov 2026
- First Element Greater Than KPHONE SCREEN · Seen Nov 2026
- Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
- Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
- Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Evaluate an Arithmetic Expression Written in WordsBrex · PHONE SCREEN
- 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
- Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesStripe · PHONE SCREENStripe interview questions
- Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
- Parse, Normalize, and Combine PolynomialsSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Polynomial CoefficientsShopify · OAShopify interview questions
- Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
- Simplify a Quadratic ExpressionOpenAI · OAOpenAI interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences