← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Senior

Senior
Apr 2026

Summary

Meta EM interview with a coding round that threw a calculator parsing problem at me. Not the most glamorous problem but it's trickier than it looks when you're on the spot.

Questions Asked (1)

Q1

Implement a basic calculator that evaluates a string expression containing non-negative integers, the operators +, -, *, /, and spaces.

Algorithms & Data Structures
Author's notes

I knew this problem category but still fumbled the operator precedence part for a minute.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that the expression is a valid infix expression with no parentheses and that division is integer division truncating toward zero. Then propose a two-pass approach: first tokenize the string, then evaluate using a stack to handle operator precedence, or use a single pass with a stack for numbers and a variable for the last operator.

Pro tip: Mention that you can solve it in one pass without a stack by keeping track of the last operator and using a running result, but using a stack is simpler and less error-prone. Also, explicitly state how you handle division by zero and integer division truncation.

1. Clarify requirements and edge cases

Ask about parentheses, unary operators, division behavior (integer vs float), and whether the expression is always valid. Confirm that only non-negative integers and the four operators are present.

2. Choose an evaluation strategy

Decide between a stack-based approach for operator precedence or a two-pass method (first handle * and /, then + and -). Explain the trade-offs in time and space complexity.

3. Implement tokenization and evaluation

Iterate through the string, building multi-digit numbers and applying operators. Use a stack to store intermediate results, handling * and / immediately and deferring + and -.

4. Handle edge cases and test

Test with expressions containing spaces, multiple digits, division truncation, and division by zero (if allowed). Walk through a few examples to verify correctness.

Key Points to Mention

  • Operator precedence: * and / have higher precedence than + and -.
  • Use of a stack to store numbers and apply operators in the correct order.
  • Handling multi-digit numbers by accumulating digits.
  • Integer division truncates toward zero (e.g., 3/2 = 1, -3/2 = -1).
  • Time complexity O(n) and space complexity O(n) for the stack.
  • Edge cases: spaces, leading/trailing spaces, division by zero, and large numbers.

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