FastPrepRemove Parentheses from a Plus-Minus Expression
Problems/Remove Parentheses from a Plus-Minus Expression

Remove Parentheses from a Plus-Minus Expression

Oracle logoOracle● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: A candidate in an Oracle cloud infrastructure loop said the phone screen asked to simplify an expression made of plus and minus signs and parentheses by taking the parentheses out. The candidate solved it and tested it with their own cases. The report gives no input format, operand form, example or output format.

Practice choices: operands are single lowercase letters; the only operators are binary plus and minus, with no unary minus, numbers or spaces; the answer keeps every operand in its original order with its final sign and does not merge repeated letters; the first operand is written with no sign; parentheses nest at most 50 levels; and the limits and all examples are ours.

The judged task matches the report at about 60%.

You are given a valid arithmetic expression expression. Each operand is a single lowercase letter, the only operators are binary + and -, and parentheses group terms. There is no unary minus.

Rewrite the expression without parentheses so that it still means the same thing. Keep every operand in its original order and give it the sign it ends up with once all the parentheses are opened: a group after - flips the sign of everything inside it, and a group after + (or at the start) changes nothing. Do not combine repeated letters.

Return the result as a string. The first operand is always positive and is written with no sign; every later operand is preceded by + or -.

Function

simplifyExpression(expression: String) → String

Examples

Example 1

expression = "a-(b-c)"return = "a-b+c"

The group (b-c) is subtracted, so every sign inside it flips: b becomes -b and -c becomes +c.

Example 2

expression = "a-(b+(c-d))+e"return = "a-b-c+d+e"

The outer group is subtracted, so b and the inner group (c-d) both flip. Inside the inner group, c was added and d subtracted; after the outer flip, c becomes -c and d becomes +d. The +e after the groups keeps its sign.

Example 3

expression = "(a+b)-(a-b)"return = "a+b-a+b"

The first group is added, so a and b keep their signs. The second group is subtracted, giving -a+b. Terms are never combined, so both a terms and both b terms stay, in their original order.

Constraints

  • 1 <= expression.length < 104
  • expression contains only lowercase English letters, '+', '-', '(' and ')'.
  • expression is a valid expression: every letter is one operand, '+' and '-' are binary operators between two terms, and every parenthesized group is non-empty and balanced.
  • Parentheses are nested at most 50 levels deep.

More Oracle problems

  • Maximum Subarray Sum with One DeletionONSITE INTERVIEW · Seen Nov 2026
  • Shortest Deletion for Distinct CharactersONSITE INTERVIEW · Seen Nov 2026
  • Count Dropped Rate-Limited RequestsONSITE INTERVIEW · Seen Nov 2026
All Oracle interview questionsSee Oracle hiring insights
Saved
class Solution {
    public String simplifyExpression(String expression) {
        // Write your code here.
    }
}
Checking account…