Evaluate Addition and Multiplication
Updated
Problem statement
Evaluate a valid arithmetic expression containing nonnegative decimal integers, binary addition +, multiplication x, and optional ASCII spaces around tokens.
Multiplication has higher precedence than addition. Leading zeros are allowed and do not change a decimal integer’s value. Spaces never split the digits of one number. Return the integer result.
The grammar contains no subtraction, division, unary signs or parentheses. There is at least one number, and each binary operator has a number on both sides.
Function
evaluateExpression(expression: String) → intExamples
Example 1
expression = "2+3x4"return = 14Multiplication is evaluated first in this practice grammar: 3x4 is 12, then 2+12 is 14.
Example 2
expression = "10x2+3x4"return = 32The two products are 20 and 12; adding them gives 32.
Example 3
expression = "7"return = 7A single literal evaluates to its own value.
Constraints
1 <= expression.length <= 10^4.- The expression contains only decimal digits, spaces,
+and lowercasexand follows the stated valid grammar. - Every literal, intermediate product, intermediate sum and final result is between
0and2147483647.
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