Fast Fourier Transform - Problem

Implement the Fast Fourier Transform (FFT) algorithm to efficiently multiply two polynomials represented as coefficient arrays.

Given two polynomials A(x) and B(x) represented by their coefficient arrays, compute their product C(x) = A(x) * B(x) and return the coefficients of the resulting polynomial.

For polynomial A(x) = a₀ + a₁x + a₂x² + ... + aₙxⁿ, the coefficient array is [a₀, a₁, a₂, ..., aₙ].

Note: The degree of the product polynomial C(x) will be deg(A) + deg(B), so if A has degree n and B has degree m, then C will have degree n+m.

Input & Output

Example 1 — Basic Linear Polynomials
$ Input: polyA = [1, 2], polyB = [3, 4]
Output: [3, 10, 8]
💡 Note: (1 + 2x)(3 + 4x) = 1×3 + 1×4x + 2x×3 + 2x×4x = 3 + 4x + 6x + 8x² = 3 + 10x + 8x²
Example 2 — Constant and Linear
$ Input: polyA = [5], polyB = [2, 1]
Output: [10, 5]
💡 Note: 5 × (2 + x) = 5×2 + 5×1×x = 10 + 5x
Example 3 — Quadratic Multiplication
$ Input: polyA = [1, 0, 1], polyB = [1, 1]
Output: [1, 1, 1, 1]
💡 Note: (1 + x²)(1 + x) = 1×1 + 1×x + x²×1 + x²×x = 1 + x + x² + x³

Constraints

  • 1 ≤ polyA.length, polyB.length ≤ 104
  • -1000 ≤ polyA[i], polyB[i] ≤ 1000

Visualization

Tap to expand
Fast Fourier Transform - Polynomial MultiplicationINPUT POLYNOMIALS12A(x) = 1 + 2x34B(x) = 3 + 4xCoefficient Arrays:polyA = [1, 2]polyB = [3, 4]Degree 1 + Degree 1→ Result Degree 2FFT ALGORITHM1Pad to power of 22Apply FFT transform3Point-wise multiply4Inverse FFT transformComplexity:Time: O(n log n)Space: O(n)Divide & conquer withcomplex exponentialslog n levels × n workRESULT3108C(x) = 3 + 10x + 8x²Result Array:[3, 10, 8]Verification:(1+2x)(3+4x)=3+4x+6x+8x²Degree = 1+1 = 2Length = 3 coefficientsKey Insight:Converting to frequency domain transforms O(n²) convolution into O(n log n) multiplication using wave symmetries and divide-and-conquer.TutorialsPoint - Fast Fourier Transform | FFT Approach
Asked in
Google 25 Microsoft 18 Apple 12 Meta 8
23.4K Views
Medium Frequency
~45 min Avg. Time
856 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