227. Basic Calculator II

Basic Calculator II (LeetCode 227) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Stack Patterns pattern, in the Expression Evaluation (RPN/Infix) subpattern.

How to solve Basic Calculator II

Basic Calculator II is a Expression Evaluation problem, part of Stack Patterns. Master expression evaluation using stack. Evaluate reverse polish notation and basic calculator problems.

Work towards the solution in these steps:

  1. Consider using a stack to help manage the operators and operands while processing the expression from left to right.
  2. Think about how operator precedence affects the evaluation order, especially for multiplication and division compared to addition and subtraction.
  3. Try parsing the expression character by character, accumulating multi-digit numbers, and handling spaces appropriately.

The reference solution runs in O(n) time and O(n) space.

The Basic Calculator II solution and editorial walks through the full approach with reference code.

Problem statement

Given a string s which represents an expression, evaluate this expression and return its value.

The integer division should truncate toward zero.

You may assume that the given expression is always valid. All intermediate results will be in the range of [-2^31, 2^31 - 1].

Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().

Example 1:

Input: s = "3+22" Output: 7

Example 2:

Input: s = " 3/2 " Output: 1

Example 3:

Input: s = " 3+5 / 2 " Output: 5

Constraints:

- 1 <= s.length <= 3 10^5

- s consists of integers and operators ('+', '-', '', '/') separated by some number of spaces.

- s represents a valid expression.

- All the integers in the expression are non-negative integers in the range [0, 2^31 - 1].

- The answer is guaranteed to fit in a 32-bit integer.

Topics and companies

Topics: Math, String, Stack.

Reported in interviews at Amazon, Facebook, Microsoft, Roblox, Snapchat, Uber.

How to practise Basic Calculator II on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(n) space.

Open Basic Calculator II in the code editor, or read the Basic Calculator II editorial for a worked solution with its approach and complexity analysis.

Where Basic Calculator II sits in the DSA pattern sheet

Problems related to Basic Calculator II

Other problems that use the same Expression Evaluation (RPN/Infix) and Stack Patterns techniques:

Basic Calculator II FAQ

What is the LeetCode number of Basic Calculator II?

Basic Calculator II is LeetCode problem 227, rated Medium.

What pattern is used to solve Basic Calculator II?

Basic Calculator II is a Expression Evaluation problem. It belongs to the Stack Patterns pattern, in the Expression Evaluation (RPN/Infix) subpattern.

What is the time complexity of Basic Calculator II?

The reference solution runs in O(n) time and O(n) space.

Which companies ask Basic Calculator II?

Basic Calculator II has been reported in interviews at Amazon, Facebook, Microsoft, Roblox, Snapchat, Uber.

Preparing this workspace.