Polynomial Coefficients
Updated
Problem statement
You are given a string polynom with no spaces. It is a quadratic expression in x, written as a sum of terms. Each term has one of three forms:
- a plain number, for example
4(a constant term); - a number followed by
x, for example5x; - a number followed by
x^2, for example7x^2, which means 7 times x squared.
A form can be used by several terms, and the terms can be written in any order. Combine the terms of each form and return three integers in an array: the coefficient of x^2, then the coefficient of x, then the constant.
How the input is written:
- There is a
+or-sign between every two terms. The first term can start with+, with-, or with no sign, and no sign means+. - The number in front of
xorx^2can be left out, and then it is1. Sox^2is the same as1x^2, and-xis the same as-1x. - Numbers are written in base 10 with no extra leading zeros. A number can be
0, as in0x. - The square is always written as
x^2. No other powers or symbols appear.
If a form has no terms, or its terms add up to zero, its place in the answer is 0. For example, an expression whose terms all cancel gives [0, 0, 0].
Function
compute(polynom: String) → int[]Examples
Example 1
polynom = "5x+7x^2+4+x^2-24x"return = [8,-19,4]The x^2 terms are 7x^2 and x^2, so they give 7 + 1 = 8 for the first place in the answer. The x terms are 5x and -24x, giving 5 - 24 = -19. The only number on its own is 4. The three places give [8, -19, 4].
Example 2
polynom = "-x^2+3-x"return = [-1,-1,3]The first term starts with a minus sign, so -x^2 counts as -1 times x^2. In the same way, -x counts as -1 times x. The number on its own is 3, so the answer is [-1, -1, 3].
Example 3
polynom = "+12x^2-0x-12x^2"return = [0,0,0]A plus sign is also allowed before the first term. 12x^2 and -12x^2 cancel, and -0x adds nothing. There is no number on its own, so every coefficient is 0 and the answer is [0, 0, 0].
Example 4
polynom = "1000000x+1000000x-1000000"return = [0,2000000,-1000000]Each number in the input is at most 1000000, but a sum can be larger: the x coefficient is 1000000 + 1000000 = 2000000. There is no x^2 term, so the answer is [0, 2000000, -1000000].
Constraints
1 <= polynom.length <= 50polynomcontains only digits,x,^,+and-, and follows the rules above.- Every number written in
polynomis at most10^6, so the coefficient of each single term is between-10^6and10^6.
More Shopify Online Assessment questions
- Card Game: Best First-Player ScoreOA · Seen Nov 2026
- Macaron Order with Set DiscountsOA · Seen Nov 2026
- Choose Items for a BackpackOA · Seen May 2026
- Filter Words Containing a LetterOA · Seen Aug 2025
- Paint HouseOA · Seen Aug 2025
- Longest Increasing SubsequenceOA · Seen May 2025
- Find the Compromised ServerOA · Seen Apr 2025
- Fix the First Robot InstructionOA · Seen Dec 2024
- Validate a Directed GraphOA · Seen Mar 2018
- Min StackOA
Similar questions at other companies
- Simplify a Quadratic ExpressionOpenAI · OAOpenAI interview questions
- Balance a Chemical EquationMicrosoft · OAMicrosoft interview questions
- Is This Date a Weekend?Salesforce · OASalesforce interview questions
- Smallest Value for a Linear Expression ModuloIntel · OA
- Evaluate an Arithmetic Expression Written in WordsBrex · PHONE SCREEN
- Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
- 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
- Solve a Linear Equation in xNutanix · PHONE SCREENNutanix interview questions