Parse, Normalize, and Combine Polynomials
Updated
Problem statement
You are given one or more polynomial strings in polynomials. Each one is valid, uses the single variable x, and has no whitespace. A polynomial is a series of signed terms. A term is either an integer constant, or an integer coefficient followed by x and an optional nonnegative exponent. Some terms: -7, x, -3x, 12x^4. A missing coefficient means 1, and a missing exponent also means 1.
Add all the polynomials together and return the sum as one string in this fixed form:
- Combine terms that have the same exponent.
- Leave out terms whose coefficient is zero.
- List the terms from the highest exponent to the lowest.
- On terms that contain
x, do not write a coefficient of1or-1. - Write the signs in normalized form, with no whitespace.
- If every term cancels, return
0.
Every coefficient you read, and every coefficient you get while adding, must fit in a signed 64-bit integer. If a coefficient you read or the result of an addition falls outside that range, stop and return exactly OVERFLOW.
Function
combinePolynomials(polynomials: String[]) → StringExamples
Example 1
polynomials = ["3x^2-x+4","-x^2+5x-4"]return = "2x^2+4x"Terms with the same power of x are added together. The two constants cancel, so the sum has no constant term.
Example 2
polynomials = ["x^3-x","-x^3+x"]return = "0"Each term in the first polynomial has its opposite in the second. Every coefficient cancels, so the answer is zero.
Example 3
polynomials = ["9223372036854775807","1"]return = "OVERFLOW"The first constant is already the largest value a signed 64-bit integer can hold. Adding the second constant goes past that limit, so the answer is OVERFLOW.
Constraints
1 <= polynomials.length <= 200.- Every expression is valid under the grammar above and has length from
1through2000. - Coefficient magnitudes contain at most
20decimal digits. - Every exponent is from
0through10^6.
More Squarepoint Capital Phone Screen questions
- Lazy Filter over a GeneratorPHONE SCREEN · Seen Nov 2026
- Moving Average of a Data StreamPHONE SCREEN · Seen Nov 2026
- Newton Square Root to a Guaranteed TolerancePHONE SCREEN · Seen Nov 2026
- Probability of Consecutive HeadsPHONE SCREEN · Seen Nov 2026
- Three Points in a Semicircle: Monte Carlo EstimatePHONE SCREEN · Seen Nov 2026
- Combine Two Index SequencesPHONE SCREEN · Seen Aug 2026
- Sorted Insertion SnapshotsPHONE SCREEN · Seen Aug 2026
- Strong and Weak Reference LifecyclePHONE SCREEN · Seen Aug 2026
- Target Sum Across Two Sorted ArraysPHONE SCREEN · Seen Aug 2026
- Classify Heap Addresses from a Memory SnapshotPHONE SCREEN · Seen Nov 2025
Similar questions at other companies
- Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesStripe · PHONE SCREENStripe interview questions
- Spreadsheet With Dependent CellsCharacter.AI · PHONE SCREEN
- Custom Record and Free-Form Field ParserNetflix · PHONE SCREENNetflix interview questions
- Toy Language Generic Return-Type InferenceOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Evaluate an Arithmetic Expression Written in WordsBrex · PHONE SCREEN
- Video Cache Hit Rates from a CSV LogTubi · PHONE SCREEN
- Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
- Employee Count Median, Mean and ModeClay · PHONE SCREEN