FastPrepBasic Calculator II With Empty Input
Problems/Basic Calculator II With Empty Input

Basic Calculator II With Empty Input

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report describes a Basic Calculator II variant with addition, subtraction, multiplication, division, and an extra rule for empty input. We chose the usual operator precedence, left-to-right grouping, division that truncates, how whitespace is handled, the function signature, the examples, and the input limits. Our version is about 78% the same as the reported question.

Evaluate an arithmetic expression containing non-negative integers, spaces, and the binary operators +, -, *, and /. Do multiplication and division before addition and subtraction. Within either pair of operations, work from left to right.

After each integer division, discard the fractional part, rounding toward zero. The expression contains no parentheses. If expression is empty or contains only spaces, return 0.

Function

calculate(expression: String) → int

Examples

Example 1

expression = "3+2*2"return = 7

Multiplication is evaluated first, so the result is 3 + 4 = 7.

Example 2

expression = " 14-3/2 "return = 13

3 / 2 truncates to 1, giving 14 - 1.

Example 3

expression = "   "return = 0

A spaces-only expression is the explicit empty case.

Constraints

  • 0 <= expression.length <= 200000.
  • After spaces are removed, a non-empty expression alternates non-negative base-10 integers and operators from +, -, *, and /.
  • No divisor is zero.
  • Every intermediate and final result fits a signed 32-bit integer.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
  • Basic Calculator IITypeSafe AI · OA
  • Basic Calculator IISnap Inc. · PHONE SCREENSnap Inc. interview questions
  • Adjacent Equal-Pair Removal GameZipRecruiter · OAZipRecruiter interview questions
  • Evaluate Arithmetic with Operator PrecedenceRoblox · PHONE SCREENRoblox interview questions
  • Simplify a Formula with Known and Unknown VariablesBetter.com · PHONE SCREEN
  • Evaluate Parenthesized Addition and SubtractionAtlassian · PHONE SCREENAtlassian interview questions
  • Smallest Value for a Linear Expression ModuloIntel · OA
  • Adjacent Pair and Run RemovalWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
  • Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions

Interview reports that mention this question

  • Google PhD AI/ML SWE Coding and ML Domain Rounds: 1 Experience

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int calculate(String expression) {
        // write your code here
    }
}
Checking account…