Katkat

Evaluate arithmetic expression without parentheses

MetaSoftware Engineer · Onsite

Last 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(

  1. 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.