Multiply Two Polynomials - Problem
Polynomial Multiplication Challenge

You're working with mathematical polynomials represented as integer arrays. Given two polynomials A(x) and B(x) where each array element at index i represents the coefficient of xi, your task is to compute their product R(x) = A(x) × B(x).

Example: If poly1 = [1, 2, 3] represents A(x) = 1 + 2x + 3x² and poly2 = [4, 5] represents B(x) = 4 + 5x, then their product should be R(x) = 4 + 13x + 22x² + 15x³, returned as [4, 13, 22, 15].

Your goal: Return an integer array representing the coefficients of the product polynomial, where the result has length (poly1.length + poly2.length - 1).

Input & Output

example_1.py — Basic Multiplication
$ Input: poly1 = [1, 2, 3], poly2 = [4, 5]
Output: [4, 13, 22, 15]
💡 Note: A(x) = 1 + 2x + 3x², B(x) = 4 + 5x. Product: (1×4) + (1×5 + 2×4)x + (2×5 + 3×4)x² + (3×5)x³ = 4 + 13x + 22x² + 15x³
example_2.py — Single Coefficient
$ Input: poly1 = [1, 2], poly2 = [3]
Output: [3, 6]
💡 Note: A(x) = 1 + 2x, B(x) = 3. Product: 3(1 + 2x) = 3 + 6x
example_3.py — Zero Coefficient
$ Input: poly1 = [0, 1], poly2 = [1, 0, 1]
Output: [0, 0, 1, 0]
💡 Note: A(x) = x, B(x) = 1 + x². Product: x(1 + x²) = x + x³, represented as [0, 1, 0, 1] but result length is 4

Constraints

  • 1 ≤ poly1.length, poly2.length ≤ 100
  • -100 ≤ poly1[i], poly2[i] ≤ 100
  • Both input arrays represent valid polynomials

Visualization

Tap to expand
Polynomial Multiplication DanceClass A: [1, 2, 3]123Class B: [4, 5]45Result Powers: [x⁰, x¹, x², x³]4x⁰132215Dance Pairs & Contributions:1×4 = 4 → x⁰ position1×5 = 5 → x¹ position2×4 = 8 → x¹ position2×5 = 10 → x² position3×4 = 12 → x² position3×5 = 15 → x³ positionFinal sums:x⁰: 4x¹: 5 + 8 = 13x²: 10 + 12 = 22x³: 15
Understanding the Visualization
1
Setup Result Array
Prepare slots for all possible power combinations (0 to n+m-2)
2
Distribute Pairs
Each student from class 1 dances with each student from class 2
3
Accumulate Contributions
All pairs contributing to same power level combine their scores
4
Final Result
Each position contains the sum of all contributions to that power
Key Takeaway
🎯 Key Insight: When multiplying polynomials, each coefficient pair contributes to exactly one power of x, determined by adding their positions (i + j). This natural mapping makes the nested loop approach both intuitive and optimal!
Asked in
Google 25 Amazon 18 Microsoft 15 Apple 12
28.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen