DIY: Basic Calculator III

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

Problem statement

You are given a string s containing only positive integers, operators ('+', '-', '*', '/'), and parentheses '()'. The intermediate results of the given expression string are in the range [-2312^{31}, 2312^{31} - 1]. The expression is guaranteed to be valid and the integer division truncates towards zero. Your task is to evaluate the given expression string.


  • 11 <= expression <= 10410^4
  • The string consists of only digits, '+', '-', '*', '/', '(', and ')'.


The input of the function is a string. The following is an example input:



The following is the output of the above input:


Coding exercise

Implement the string_calculate(s) function, where the string s is a mathematical expression and returns the integer number.

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