Simplify a Quadratic Expression
Reported by candidates from OpenAI's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The OpenAI OA reported in September 2026 looks easy, and that's the trap. You get a string like "5x+7x^2+4+x^2-24x" and return three coefficients. No fancy algorithm. The whole solution hinges on a tiny fixed-size array of three buckets, one each for x^2, x, and the constant. Parse the string left to right, drop each term into the right bucket, done. Where people lose points is the parsing: bare x, +x, -x^2, and signs glued to the next term. If you blank on the tokenizer mid-assessment, StealthCoder runs invisibly on your desktop as a safety net while you work.
The problem
Given a valid string polynom representing a quadratic expression, combine like terms and return the final coefficients. The expression is a sum of terms in any order. Each term is one of: a constant such as 4 or -12; a linear term such as 5x, -x, or +x; a quadratic term such as 7x^2, -x^2, or +x^2. A missing coefficient before x means 1. Return an array [a, b, c], where the simplified expression is ax^2 + bx + c. Function simplifyQuadratic(polynom: String) → int[] Examples Example 1 polynom = "5x+7x^2+4+x^2-24x" return = [8,-19,4] The quadratic coefficient is 7 + 1 = 8, the linear coefficient is 5 - 24 = -19, and the constant is 4. Example 2 polynom = "x^2-x+1" return = [1,-1,1] The omitted coefficients of x^2 and -x are 1 and -1. Example 3 polynom = "-3+2x^2+3-2x^2" return = [0,0,0] Both the quadratic terms and the constant terms cancel. Constraints 1 <= polynom.length <= 100000. polynom contains no spaces and is a valid sequence of signed constant, x, and x^2 terms. Every explicit coefficient is a base-10 integer. The final three coefficients fit in a signed 32-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a single linear pass with three accumulators. Walk the string, read a sign (default plus), then read digits into a number. Then look at what follows. If the next char is x, check for ^2. No digits before x means the coefficient is 1. Add sign times coefficient to the matching bucket. Otherwise it's a constant. The common pitfalls: treating the 2 in x^2 as a coefficient, forgetting that -x means -1, and splitting on '-' in a way that eats the sign or breaks on a leading sign. Also watch the length: 100000 characters means no repeated string concatenation or quadratic regex tricks. Use an index pointer and one pass. That's O(n) time and O(1) space. If the parsing logic slips away live, StealthCoder is the hedge that hands you a clean tokenizer fast.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Simplify a Quadratic Expression cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass OpenAI's OA.
OpenAI reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Simplify a Quadratic Expression FAQ
How hard is the OpenAI Simplify a Quadratic Expression problem really?+
Easy on algorithm, medium on care. There's no clever data structure beyond three counters. The difficulty is edge cases in parsing: implicit coefficients, leading signs, and the ^2 exponent. Write the parser slowly and test all three examples before submitting.
What's the trick to solving it?+
Keep an array of three integers and scan once. For each term, read the sign, read any digits, then check whether x follows and whether ^2 follows that. Missing digits before x means 1. Add the signed value to the right slot.
Can I just split the string on plus and minus?+
You can, but it's error-prone. A leading minus gives an empty first token, and you have to re-attach signs. A manual index scan or a regex that captures sign, digits, and x parts is cleaner. Whichever you pick, test with a leading sign and bare x.
Which edge cases should I test before submitting?+
Test x^2-x+1 for implicit coefficients, -3+2x^2+3-2x^2 for full cancellation to zeros, a string starting with a plus sign, a single constant like -12, and a very long input. Make sure 2 in x^2 never gets added as a constant.
How do I prepare for this in 48 hours?+
Practice hand-written string parsers with an index pointer, like a basic calculator. Rewrite this one from scratch twice without looking. Focus on signs and implicit values. It's a simulation and string parsing problem, so clean code beats cleverness.