← Bytedance Interview Insights

Bytedance·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Bytedance software engineer round, one coding question on expression evaluation. Pretty standard stuff but the implementation details trip you up if you haven't seen it before.

Questions Asked (1)

Q1

Implement a basic calculator that evaluates a string expression containing non-negative integers, the four arithmetic operators, and spaces, without using any built-in eval functions.

Algorithms & Data Structures
Author's notes

I knew the stack approach going in but kept second-guessing myself on when to apply multiplication and division versus just pushing things.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a stack-based approach to handle operator precedence by evaluating multiplication and division immediately and deferring addition and subtraction. Iterate through the string, building numbers and tracking the previous operator, then sum the stack at the end.

Pro tip: Clarify constraints upfront (e.g., integer division truncation, no parentheses) and discuss edge cases like leading/trailing spaces and single-number expressions. Mention that this approach can be extended to support parentheses using recursion or two stacks.

1. Clarify requirements and edge cases

Confirm with the interviewer that the expression is valid, contains only non-negative integers, +, -, *, /, and spaces, and that division truncates toward zero. Ask about potential edge cases like empty strings or single numbers.

2. Choose a stack-based strategy

Decide to use a stack to store intermediate results. Process multiplication and division immediately to respect precedence, and push addition/subtraction results onto the stack for final summation.

3. Iterate through the string

Traverse the string character by character, skipping spaces. Build multi-digit numbers, and when an operator is encountered, apply the previous operator to the current number and the top of the stack (for * and /) or push the number (for + and -).

4. Handle the final number and sum the stack

After the loop, apply the last operator to the final number. Then sum all values in the stack to get the final result.

5. Test with examples and edge cases

Walk through examples like '3+2*2', ' 3/2 ', and ' 3+5 / 2 ' to verify correctness. Discuss time and space complexity (O(n) time, O(n) space).

Key Points to Mention

  • Operator precedence: multiplication and division are evaluated before addition and subtraction.
  • Stack usage to defer addition and subtraction until the end.
  • Handling multi-digit numbers by accumulating digits.
  • Integer division truncation toward zero (e.g., 3/2 = 1, -3/2 = -1 if negatives were allowed).
  • Time and space complexity: O(n) time and O(n) space in the worst case.
  • Edge cases: leading/trailing spaces, single number, and expressions with only addition/subtraction.

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