FastPrepEvaluate a Parenthesized Integer Expression

Evaluate a Parenthesized Integer Expression

Adobe logoAdobe● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

Evaluate the integer expression in expression.

The expression contains non-negative decimal integers, the binary operators + and -, parentheses, and spaces. A + or - may also appear as a unary sign, including immediately before a parenthesized expression.

Use integer arithmetic and return the value of the complete expression. Do not call a language-provided expression evaluator.

Function

calculate(expression: String) → int

Examples

Example 1

expression = "1 + 1"return = 2

Adding the two operands gives 2.

Example 2

expression = "(1+(4+5+2)-3)+(6+8)"return = 23

The two outer groups evaluate to 9 and 14.

Example 3

expression = "-(2 + 3) + 10"return = 5

The unary minus negates the parenthesized value 5.

Constraints

  • 1 <= expression.length <= 100000
  • expression is a valid expression containing digits, spaces, +, -, (, and ).
  • Every intermediate value and the final answer fit in a signed 32-bit integer.

Source note: The source image is a source-neutral excerpt of the author-reported prompt.

More Adobe problems

See Adobe hiring insights
public int calculate(String expression) {
  // Write your code here.
}
expression"1 + 1"
expected2
Checking account…