Multiply two polynomials given as coefficient arrays

Quick Overview

Multiply two polynomials stored as integer coefficient arrays, where index i holds the coefficient of x to the power i, and return the product's coefficients. It tests array indexing, convolution of two sequences, sizing the output correctly and reasoning about time complexity.

Multiply two polynomials given as coefficient arrays

Company: NVIDIA

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given two polynomials represented as integer arrays `poly1` and `poly2`, return the coefficient array of their product. Each array represents polynomial coefficients where the index corresponds to the power of $x$. For example, `[3, 2, 1]` represents the polynomial ${3 + 2x + x^2}$. Return the resulting polynomial as an array of coefficients in the same format. ### Function Signature ```python def multiply_polynomials(poly1: list[int], poly2: list[int]) -> list[int]: ``` ### Rules - `poly1[i]` and `poly2[i]` are the coefficients of $x^i$, so index 0 is the constant term. - The result has exactly `len(poly1) + len(poly2) - 1` entries, and entry `k` is the coefficient of $x^k$ in the product. - Keep every zero coefficient, including trailing ones. For example, `poly1 = [0]` and `poly2 = [4, 5]` give `[0, 0]`. ### Constraints - `1 <= len(poly1) <= 500` - `1 <= len(poly2) <= 500` - `-1000 <= poly1[i], poly2[i] <= 1000` ### Examples ```text Input: poly1 = [1, 2], poly2 = [3, 4] Output: [3, 10, 8] ``` Here `poly1` is ${1 + 2x}$ and `poly2` is ${3 + 4x}$. Expanding gives ${3 + 4x + 6x + 8x^2 = 3 + 10x + 8x^2}$, so the coefficients are `[3, 10, 8]`. ```text Input: poly1 = [1, 1, 1], poly2 = [1, 1] Output: [1, 2, 2, 1] ``` The product $(1 + x + x^2)(1 + x)$ expands to ${1 + x + x + x^2 + x^2 + x^3 = 1 + 2x + 2x^2 + x^3}$. ```text Input: poly1 = [2, -1], poly2 = [-3, 0, 1] Output: [-6, 3, 2, -1] ``` The product $(2 - x)(-3 + x^2)$ is ${-6 + 2x^2 + 3x - x^3}$. Ordered by power, that is `[-6, 3, 2, -1]`, and the result has ${2 + 3 - 1 = 4}$ entries.

Overview: Multiply two polynomials stored as integer coefficient arrays, where index i holds the coefficient of x to the power i, and return the product's coefficients. It tests array indexing, convolution of two sequences, sizing the output correctly and reasoning about time complexity.

NVIDIA logo
NVIDIA
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given two polynomials represented as integer arrays poly1 and poly2, return the coefficient array of their product.

Each array represents polynomial coefficients where the index corresponds to the power of xx. For example, [3, 2, 1] represents the polynomial 3+2x+x2{3 + 2x + x^2}.

Return the resulting polynomial as an array of coefficients in the same format.

Function Signature

def multiply_polynomials(poly1: list[int], poly2: list[int]) -> list[int]:

Rules

  • poly1[i] and poly2[i] are the coefficients of xix^i , so index 0 is the constant term.
  • The result has exactly len(poly1) + len(poly2) - 1 entries, and entry k is the coefficient of xkx^k in the product.
  • Keep every zero coefficient, including trailing ones. For example, poly1 = [0] and poly2 = [4, 5] give [0, 0] .

Constraints

  • 1 <= len(poly1) <= 500
  • 1 <= len(poly2) <= 500
  • -1000 <= poly1[i], poly2[i] <= 1000

Examples

Input: poly1 = [1, 2], poly2 = [3, 4]
Output: [3, 10, 8]

Here poly1 is 1+2x{1 + 2x} and poly2 is 3+4x{3 + 4x}. Expanding gives 3+4x+6x+8x2=3+10x+8x2{3 + 4x + 6x + 8x^2 = 3 + 10x + 8x^2}, so the coefficients are [3, 10, 8].

Input: poly1 = [1, 1, 1], poly2 = [1, 1]
Output: [1, 2, 2, 1]

The product (1+x+x2)(1+x)(1 + x + x^2)(1 + x) expands to 1+x+x+x2+x2+x3=1+2x+2x2+x3{1 + x + x + x^2 + x^2 + x^3 = 1 + 2x + 2x^2 + x^3}.

Input: poly1 = [2, -1], poly2 = [-3, 0, 1]
Output: [-6, 3, 2, -1]

The product (2−x)(−3+x2)(2 - x)(-3 + x^2) is −6+2x2+3x−x3{-6 + 2x^2 + 3x - x^3}. Ordered by power, that is [-6, 3, 2, -1], and the result has 2+3−1=4{2 + 3 - 1 = 4} entries.