Basic Calculator II
Problem statement
Given a string $s$ representing a valid arithmetic expression, evaluate it and return the result. The expression contains non-negative integers and the operators $+$, $-$, $*$, $/$ separated by optional spaces. Integer division truncates toward zero. You may not use any built-in eval.
Constraints:
- $1 \le \mathrm{len}(s) \le 3 \times 10^5$
- $s$ is a valid expression; the answer and all intermediate values fit in a signed 32-bit integer.
Examples:
- $s = $ "3+2*2" $\to 7$
- $s = $ " 3/2 " $\to 1$
- $s = $ " 3+5 / 2 " $\to 5$
Show hint
How can you respect operator precedence in one pass without building a full expression tree?
Relevant roles: ML Engineer, Forward Deployed Engineer and AI Engineer.