Solve a Linear Equation in x
Updated
Problem statement
You are given a linear equation in one unknown, x, as a string. Solve it and return the value of x.
The equation has this form:
- There is exactly one
=, with one side of the equation on each side of it. - Each side is one or more terms joined by
+or-. The first term of a side may have a-in front of it. - A term is a whole number such as
7, a whole number followed byxsuch as3x, or justx, which means1x. - Spaces may appear before or after any term, sign or
=, but never inside a term.
The equation always has exactly one solution. Return it as a floating-point number. An answer is accepted when it is within 10^-9 of the correct value, or within 10^-9 times the correct value when that value is larger than 1 in size.
Function
solveEquation(equation: String) → doubleExamples
Example 1
equation = "3x + 4 = x - 6"return = -5.0Move the x terms to the left and the numbers to the right: 3x - x = -6 - 4, so 2x = -10 and x = -5.
Example 2
equation = "x - 7 + 2x = 10 - 4x + 1"return = 2.5714285714285716The left side collects to 3x - 7 and the right side to 11 - 4x. Then 3x + 4x = 11 + 7, so 7x = 18 and x = 18 / 7, which is about 2.5714285714285716.
Example 3
equation = "-x + 9 = 2x"return = 3.0The bare -x means -1x. Moving 2x to the left and 9 to the right gives -3x = -9, so x = 3.
Constraints
3 <= equation.length <= 1000- Every number in
equationis a whole number from 0 to 1000, written without leading zeros. equationfollows the form described above.- After collecting terms, the number of
xon the left differs from the number on the right, so there is exactly one solution.
More Nutanix Phone Screen questions
- Swap Adjacent Linked List NodesPHONE SCREEN · Seen Mar 2025
- Top K Most Frequent ValuesPHONE SCREEN · Seen Mar 2025
- Unique Triplets Summing to ZeroPHONE SCREEN · Seen Sep 2024
- Find the Duplicate in a Read-Only ArrayPHONE SCREEN · Seen Apr 2021
- Kth Largest Element in an ArrayPHONE SCREEN · Seen Feb 2020
- Kth Smallest Element in a BSTPHONE SCREEN · Seen Feb 2020
- Outline of a Binary TreePHONE SCREEN · Seen Jan 2020
- Print All File and Folder PathsPHONE SCREEN · Seen Dec 2019
- Inorder Predecessor in a BSTPHONE SCREEN · Seen Jan 2019
- K Largest and K Smallest ValuesPHONE SCREEN · Seen Jan 2019
Similar questions at other companies
- Evaluate an Arithmetic Expression Written in WordsBrex · PHONE SCREEN
- Evaluate a Flat Expression or Report Invalid InputMeta · PHONE SCREENMeta interview questions
- Strict Text to Integer for a Given SizeAdobe · PHONE SCREENAdobe interview questions
- Evaluate an Arithmetic ExpressionAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
- Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesStripe · PHONE SCREENStripe interview questions
- Parse, Normalize, and Combine PolynomialsSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
- Simplify a Quadratic ExpressionOpenAI · OAOpenAI interview questions
- Balance a Chemical EquationMicrosoft · OAMicrosoft interview questions