DIY: Basic Calculator II

Solve the interview question "Basic Calculator II" in this lesson.

Problem statement

In this challenge, you are given a string, st, which contains an expression. You’ll evaluate this expression and return its value. Keep the following statements in mind while solving the problem:

  • The integer division should be rounded off to the nearest smallest integer.
  • The given expression is always valid, giving the intermediate result in the range of [[-2312^{31}, 2312^{31} - 1]1].
  • You are not allowed to use any built-in function that evaluates the string as a mathematical expression.

Constraints:

  • st will contain an expression made up of integers and operators (*,/, +, -) and separated by some number of spaces.
  • The expression will contain non-negative integers in the range [0[0, 2312^{31} - 1]1].
  • 1<=1 <= st.length <=3×105<= 3 \times 10^5

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.