Simplify a Nested Variable Expression
Updated
Problem statement
Given a valid arithmetic expression containing lowercase one-letter variables, +, -, parentheses, and optional spaces, combine like variables and return an equivalent simplified expression.
A term is a variable or a parenthesized expression. An expression may begin with a unary + or -, and its remaining terms are joined by binary + or -. Spaces may occur between tokens. There are no numbers in the input.
In the output, list nonzero negative-coefficient variables first in alphabetical order, then nonzero positive-coefficient variables in alphabetical order. Use - before a negative term and + before a positive term, with one space on each side of every binary sign. Omit the coefficient when its absolute value is one; otherwise write the magnitude as m*variable. Return 0 if all terms cancel.
Function
simplifyExpression(expression: String) → StringExamples
Example 1
expression = "a - (b + a - (c + a))"return = "-b + a + c"The outer minus reverses the signs inside the parentheses. The two cancelling a terms leave one positive a.
Example 2
expression = "a + b - a"return = "b"The positive and negative a terms cancel.
Example 3
expression = "-(x - (y + x))"return = "y"Negating the outer group leaves -x + y + x, so only y remains.
Example 4
expression = "a - a"return = "0"All variable coefficients become zero.
Constraints
1 <= expression.length <= 100000.- The input is a valid expression of one-letter variables, unary or binary signs, parentheses and spaces as described above.
- Every variable is a lowercase English letter.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
- Basic Calculator IISnap Inc. · PHONE SCREENSnap Inc. interview questions
- Evaluate Arithmetic with Operator PrecedenceRoblox · PHONE SCREENRoblox interview questions
- Simplify a Formula with Known and Unknown VariablesBetter.com · PHONE SCREEN
- Evaluate Parenthesized Addition and SubtractionAtlassian · PHONE SCREENAtlassian interview questions
- Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
- Adjacent Equal-Pair Removal GameZipRecruiter · OAZipRecruiter interview questions
- Smallest Value for a Linear Expression ModuloIntel · OA
- Adjacent Pair and Run RemovalWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Evaluate an Arithmetic Expression Written in WordsBrex · PHONE SCREEN