FastPrepSolve a Linear Equation in x
Problems/Solve a Linear Equation in x

Solve a Linear Equation in x

Nutanix logoNutanix● MediumINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: An intern applicant's report says it was written minutes after a Nutanix phone interview in October 2018. It pastes the task as a short English code comment. The task is to find x in a one-variable linear equation given as a string. The comment has a few limits on what the equation may contain, two sample equations, and parentheses as an optional extension.

We kept the comment's limits: whole-number coefficients, plus and minus only, x returned as a double, and no need to handle an equation like x = x. The exact text format is ours. Terms look like 7, 3x or x. A minus is allowed in front of the first term of a side. Spaces are allowed anywhere except inside a term. We also require exactly one solution, which rules out equations with none. We leave parentheses out of the tested task, and the editorial discusses them. We chose the limits (numbers from 0 to 1000, 3 to 1000 characters), the answer tolerance and the examples.

Our version is about 85% the same as the reported question.

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 by x such as 3x, or just x, which means 1x.
  • 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) → double

Examples

Example 1

equation = "3x + 4 = x - 6"return = -5.0

Move 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.5714285714285716

The 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.0

The 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 equation is a whole number from 0 to 1000, written without leading zeros.
  • equation follows the form described above.
  • After collecting terms, the number of x on 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

Interview reports that mention this question

  • Nutanix SWE Intern Phone Screens: 20 Experiences (Part 3 of 3)

Nutanix interview experiences

  • Nutanix SWE Intern OA Questions: 16 Experiences (Part 2 of 3)
  • Nutanix U.S. SWE Intern Interviews: 54 Experiences (Part 1 of 3)
  • Nutanix SWE OA and Interviews, Level Not Reported: 9 Experiences
  • Nutanix Intern Debugging Round and Interviews: 25 Experiences
All Nutanix interview questionsSee Nutanix hiring insights
Saved
class Solution {
    public double solveEquation(String equation) {
        // Write your code here.
    }
}
Checking account…