Prepin
Log in

Evaluate the outcome of a mathematical expression presented in string format.

Approach / Explanation

This question requires you to calculate the result of a mathematical expression provided in the form of a string. The string may include integers, as well as the operators for addition (+), subtraction (-), multiplication (*), and division (/), along with parentheses (()). The division operator (/) performs a floor division, meaning the outcome is the largest integer that is less than or equal to the division result. Your algorithm must adhere to the standard order of operations, known as BODMAS or PEDMAS, which dictates that calculations inside parentheses are done first, followed by exponentiation, then multiplication and division, and finally addition and subtraction (in left-to-right order). Using any built-in function or library to evaluate strings as mathematical expressions is not permitted.

Suggested Answer

A stack-based method can be employed to address this issue. Below is a general outline of a solution in Python: Begin by initializing two stacks: one for numbers and another for operators. Read the string from left to right: If the current character is a digit, continue scanning to form the entire number and push it onto the values stack. If the character is an opening parenthesis '(', add it to the operators stack. If the character is a closing parenthesis ')', evaluate the expression enclosed within the parentheses. If the character is an operator, manage the precedence and process the top operators while the operator on top of the stack has the same or higher precedence. After parsing the entire string, execute any remaining operations. You will need to implement two helper functions: The function isGreaterPrecedence(op1, op2) determines if 'op1' has a higher precedence than 'op2'. The function calculate(op, v1, v2) carries out the operation 'op' using the operands 'v1' and 'v2'. For instance: Input string: "2*(3+6/2)-4" Output: 8

Alternative Answer

Here is another illustration of the aforementioned method: Input string: "3*(2+2)-6" Output: 6 Below is a concise explanation of this example: Read "3"; since it's a number, add it to the values stack. Read "*"; being an operator, add it to the operators stack. Read "(", which is also an operator, and push it onto the operators stack. Read "2"; it's a number, so push it to the values stack. Read "+"; as an operator, push it to the operators stack. Read "2"; push this number onto the values stack as well. Read ")"; as this is a closing bracket, we first evaluate the expression within the brackets, "2+2", and push the result "4" onto the values stack. Read "-"; this is an operator. Now, we solve the remaining operations within the parentheses, "3*4", which gives us "12", and we push this result to the values stack. Then, we add "-" to the operators stack. Read "6"; push this number to the values stack. Now that the entire string has been parsed, we proceed to evaluate the remaining operations in the stack, "12-6". Ultimately, this results in "6".

Question Details

Difficulty & Category

Medium
algorithms

Companies

Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.