Evaluate arithmetic expression without parentheses
MetaSoftware Engineer · OnsiteLast updated
Evaluate arithmetic expression without parentheses evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Write evaluate(s: string) to compute the value of an arithmetic expression containing non-negative integers, '+', '-', '', '/', and spaces, with operator precedence ( and / before + and -) and no parentheses. Division truncates toward zero. Aim for a single pass with O(
- extra space beyond the output and a small stack or running registers. Discuss handling multi-digit numbers, consecutive spaces, and edge cases such as '3/2', '14-3/2', and trailing operators being invalid. Provide unit tests.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.