Solve the Equation

MediumStringssimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a linear equation in one variable x as a string, for example "x+5-3+x=6+x-2". Each side of the = is a sum of terms joined by + or -. A term is either a non-negative integer (like 12) or an optional non-negative integer coefficient followed by x (like x, 3x, or 0x). The first term on either side may start with a -. Return: - "x=#" where # is the value of x, if the equation has exactly one solution (it is guaranteed to be an integer), - "No solution" if no value of x works, - "Infinite solutions" if every value of x works.

Examples

Input: equation = "x+5-3+x=6+x-2"
Output: "x=2"
The left side is `2x+2` and the right side is `x+4`, so `x = 2`.
Input: equation = "x=x"
Output: "Infinite solutions"
Both sides are identical.
Input: equation = "x=x+2"
Output: "No solution"
Subtracting `x` gives `0 = 2`.

Constraints

  • `3 <= equation.length <= 1000`
  • `equation` contains exactly one `=`
  • Terms are integers without leading zeros, optionally followed by `x`, joined by `+` or `-`
  • If the equation has a unique solution, it is an integer with absolute value at most `1000`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems