The "Basic Calculator III interview question" is the ultimate evolution of the expression evaluation series. It combines the challenges of its predecessors: it includes non-negative integers, the four basic operators (+, -, *, /), and parentheses ( and ). Your task is to evaluate the entire expression while strictly following the order of operations (PEMDAS/BODMAS).
Top-tier companies like Meta, Google, and Amazon use the "Basic Calculator III coding problem" to identify candidates with exceptional logical structured thinking. This problem requires a perfect blend of recursion and stack-based management. It tests your ability to break a complex problem into nested, solvable sub-problems, mirroring how real-world compilers and interpreters function.
This problem is best solved using a combination of the Stack and Recursion patterns.
(, it calls itself recursively to solve the sub-expression. When it hits ), it returns the result of the current scope.Expression: 2 * (5 + 5 / 2)
2. last_op = '*'.(. Recursive call for 5 + 5 / 2.
5. last_op = '+'.+. Push 5. last_op = '+'.5. last_op = '/'./. Push 5. last_op = '/'.2. last_op was /, pop 5, divide by 2, push 2 (integer division).last_op was *, multiply 2 by returned 7.If you can solve "Basic Calculator III," you have mastered the "Stack interview pattern." Focus on writing modular code—have a clear section for number parsing, operator handling, and recursive calls. This makes your logic much easier to communicate to an interviewer.