Evaluate an Arithmetic Expression Without Parentheses
Asked at Meta · Coding Questions · Difficulty: Medium
Evaluate an arithmetic expression string containing only non-negative integers, spaces, and the operators `+ - * /` (no parentheses). `*` and `/` bind tighter than `+` and `-`; equal-precedence operators evaluate left to right; `/` is real-number division. Return the numeric result (as an `int` when it is integral, otherwise a `float`), or the string `"Invalid"` for any malformed expression (bad characters, misplaced operators such as `1++2` or a leading operator, division by zero, or an empty expression). Unary signs are not supported.
Loading the problem…