Remove parentheses from a plus/minus expression
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Google and Meta. It tests stack, parsing and strings.
Write a function simplify(expr: str) -> str that removes every parenthesis from an expression and gives each term the sign it really has.
Input. A valid expression made of:
- operands: variable names (letters, digits and
_, starting with a letter, such asaorx1) and non-negative integers - the binary operators
+and- - unary minus at the start of the expression or right after
( - parentheses, nested to any depth
- spaces, which you should ignore
There is no multiplication or division.
Output. An equivalent expression with no parentheses and no spaces:
- Keep the terms in the order they appear in the input.
- Each term has the sign it gets once all enclosing groups are expanded.
- Drop a leading
+, but keep a leading-. - Do not combine like terms:
x-(x)becomesx-x. - If the expression has no operands, return
"0".
Constraints. 1 <= len(expr) <= 10^5. Nesting depth can be close to len(expr), so avoid deep recursion. Aim for O(n) time.
Example 1
simplify("m-(n-(p+q))") -> "m-n+p+q"
p and q are inside two groups that each have a - in front. The two minus signs cancel.
Example 2
simplify("-(u - 4) + (-(v))") -> "-u+4-v"
Part 2 (follow-up). Write simplify_combined(expr), which also combines like terms and sums the integers into one constant at the end. For example, "k-(j-k)+5-2" becomes "2k-j+3".
Clarifying questions
- Can a unary minus appear before an operand other than at the start or after
(, as ina+-b? - Are empty groups like
()possible, and how do they count toward "no operands"? - In Part 2, how are coefficients written:
2k,-k, and is a zero term dropped? - In Part 2, if everything cancels, is the answer
"0"?
What a strong answer covers
- Scans once, keeping a stack of group signs: at
(push the current sign times the sign before the group, at)pop. - Emits each operand with the product of its own sign and the enclosing group's sign, dropping a leading
+. - Tokenizes multi-character names and integers, skips spaces, and stays iterative for deep nesting:
O(n)time. - Part 2: accumulates coefficients per variable in order of first appearance plus one constant, then formats them, omitting a coefficient of
1and terms with0. - Tests both examples, deeply nested minus signs, a lone integer, and an expression that cancels to
0.
Follow-ups
- Part 2: combine like terms and sum the integers.
- Add multiplication by integer constants, such as
2*(a-b). - Evaluate the expression given values for the variables.
Where it's asked
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.

