FastPrepSimplify a Formula with Known and Unknown Variables
Problems/Simplify a Formula with Known and Unknown Variables

Simplify a Formula with Known and Unknown Variables

Better.com logoBetter.com● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Better.com full-time phone screen. The calculator skill in the report is about 85% the same as ours, based on the detailed task that the report links to. The report's first recollection said there were no subexpressions. That conflicts with the link to the exact question. So we follow the link's stronger example, which has parentheses and symbols that get replaced. We chose the grammar, the limits, how coefficients are added up, and how each term is written in one fixed form. One occurrence is kept. Pure numeric expressions are a special case.

You are given an arithmetic expression and two arrays, names and values, that list the known variables. They line up by index: the value at an index in values belongs to the name at the same index in names. Simplify the expression as far as you can. Put in the value of every known variable, work out the number parts, and keep every unknown variable as a symbol.

The expression may contain decimal literals (numbers written in digits), lowercase variable names, + and - between two values, and parentheses. A minus sign may also sit right before a number, a variable or a group in parentheses to negate it. Spaces may appear between tokens. The input is always valid. There is no multiplication or division, and you do not need to handle invalid input. An expression may also be only numbers, with no parentheses.

The known names are unique, and their values are integers. A known name does not have to appear in the expression. Each unknown variable is its own symbol, independent of the others.

If an unknown variable appears more than once, add its uses up into one integer coefficient. Return the result as a string in this form:

  • If the constant is not zero, write it first.
  • Then write each unknown variable whose coefficient is not zero, in alphabetical order by name.
  • Put + or - between terms. Do not start the string with a plus sign.
  • When a coefficient has absolute value 1, write just the name. For a larger absolute value, write coefficient*name.
  • Leave out every zero term. If nothing is left, return 0.

Function

simplifyFormula(expression: String, names: String[], values: int[]) → String

Examples

Example 1

expression = "(e+3)-(pressure+temperature)+2"names = ["e","y","pressure"]values = [8,7,5]return = "8-temperature"

The known e and pressure are replaced by their values, and the numbers combine. temperature is unknown, so it stays as a symbol. The answer is 8-temperature. The known variable y is never used, which is allowed.

Example 2

expression = "a+a-b+4"names = []values = []return = "4+2*a-b"

Both uses of a combine into one term, and b stays as it is. The constant goes first, then the variables in alphabetical order: 4+2*a-b.

Example 3

expression = "12-5+1"names = []values = []return = "8"

There are no variables here, only numbers. Working them out from left to right gives 8.

Constraints

  • 1 <= expression.length <=500, and parentheses nest at most 50 deep.
  • Each variable name has 1 to 20 lowercase English letters. The expression uses at most 25 different names, and there are at most 25 known names.
  • Every number in the expression is from 0 to 1000000. Known values are from -1000000 to 1000000.
  • The input is valid, the two arrays have the same length, and the known names are unique. Every value during the calculation fits in a signed 32-bit integer.

Similar questions at other companies

  • Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
  • Reorderable Task Scheduler With CooldownWaymo · PHONE SCREENWaymo interview questions
  • Basic Calculator IISnap Inc. · PHONE SCREENSnap Inc. interview questions
  • Text Embedding and Vector SearchNike, Inc. · PHONE SCREEN · ONSITE INTERVIEWNike, Inc. interview questions
  • Per-Segment SpeedingOnePay · PHONE SCREENOnePay interview questions
  • Find Accounts with Negative Converted BalancesStripe · PHONE SCREENStripe interview questions
  • Rank Bipedal Dinosaurs by SpeedMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Configurable Valid BracketsMaven Clinic · PHONE SCREEN · ONSITE INTERVIEWMaven Clinic interview questions
  • Count Sentences From Anagram SwapsAdobe · PHONE SCREENAdobe interview questions
  • Simplify a Nested Variable ExpressionGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
See Better.com hiring insights
Saved
class Solution {
    public String simplifyFormula(String expression, String[] names, int[] values) {
        // Write your code here.
        return "";
    }
}
Checking account…