← Meta Interview Insights

Meta·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Meta SWE coding round with a calculator problem. Pretty standard stuff but the operator precedence angle is where people slip up if they're not careful.

Questions Asked (1)

Q1

Given a string expression containing non-negative integers, '+', '*', and spaces, evaluate it correctly respecting operator precedence (multiplication before addition, no parentheses).

Algorithms & Data Structures
Author's notes

The multiplication-before-addition precedence is the whole point of the problem and it's easy to just iterate left-to-right and get the wrong answer on something like '3+2*2'.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that the expression contains only non-negative integers, '+', '*', and spaces, with no parentheses, and that standard operator precedence applies. Then propose a single-pass stack-based solution that processes multiplication immediately and defers addition, or a two-pass approach that first evaluates all multiplications and then sums the results. Walk through a small example to demonstrate correctness and discuss time/space complexity.

Pro tip: Mention that you can avoid a stack by maintaining a running sum and a current term, updating the term on '*' and adding it to the sum on '+' or at the end. This shows you can optimize space to O(1) while keeping the code clean and interview-friendly.

1. Clarify requirements and edge cases

Confirm that the expression is valid, contains only non-negative integers, '+', '*', and spaces, and that operator precedence applies with no parentheses. Ask about input size, potential overflow, and whether the result fits in a standard integer.

2. Choose an approach

Decide between a stack-based single-pass evaluation or a two-pass approach (first multiply, then add). Explain the trade-offs: stack uses O(n) space but is straightforward; two-pass may require extra space for tokens but can be simpler to reason about.

3. Outline the algorithm

For the stack approach: iterate through the string, build numbers, and when an operator is encountered, apply the previous operator. For '*', pop the last number, multiply, and push the result; for '+', push the number. Finally, sum the stack. For the O(1) space approach: maintain a running sum and a current term, updating the term on '*' and adding it to the sum on '+' or at the end.

4. Walk through an example

Trace the algorithm on a sample expression like '3 + 2 * 2' to show how multiplication is handled before addition. Highlight how the stack or running sum evolves step by step.

5. Analyze complexity and test

State that the time complexity is O(n) for a single pass (or O(n) for two passes) and space complexity is O(n) for the stack or O(1) for the optimized approach. Discuss potential edge cases like leading/trailing spaces, multiple-digit numbers, and expressions with only one number.

Key Points to Mention

  • Operator precedence: multiplication before addition
  • Single-pass stack-based evaluation or two-pass approach
  • Handling multi-digit numbers and spaces
  • Time complexity O(n) and space complexity O(n) or O(1)
  • Edge cases: single number, no operators, large numbers (overflow)
  • Code clarity and modularity (e.g., helper function to apply operator)

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.