← Questions
Polynomial multiplication API in C
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Nvidia's technical phone screen, last asked Feb 2026. It tests c++, api design and memory management.
Design and implement a small C API for polynomials with integer coefficients: a type, functions to create and free a polynomial, and a function that multiplies two polynomials and returns a new one. Pay attention to memory ownership and error handling, as in a production C library.
Interface
Unless you agree on a different interface with the interviewer, assume:
typedef struct poly poly_t; /* opaque */
poly_t *poly_create(const long *coeffs, size_t n); /* coeffs[i] is the x^i term; copies them */
void poly_free(poly_t *p); /* NULL is allowed */
size_t poly_degree(const poly_t *p);
long poly_coeff(const poly_t *p, size_t i); /* 0 beyond the degree */
poly_t *poly_mul(const poly_t *a, const poly_t *b); /* new polynomial owned by the caller, NULL on error */
Example
a = 1 + 2x coeffs {1, 2}
b = 3 - x + x^2 coeffs {3, -1, 1}
poly_mul(a, b) = 3 + 5x - x^2 + 2x^3 coeffs {3, 5, -1, 2}
(1 - x) * (1 + x) = 1 - x^2 coeffs {1, 0, -1}
Constraints
- Degrees up to about 10^4 in the dense form.
- Coefficients may be zero or negative; trailing zero coefficients are trimmed so the degree is exact.
Clarifying questions
- Dense arrays or sparse (exponent, coefficient) terms?
- Who owns the result, and must inputs stay unmodified?
- How are allocation failure and overflow signalled to the caller?
- How is the zero polynomial represented, and what is its degree?
What a strong answer covers
- An opaque struct with a length and a heap array, allocated with checks; clear ownership rules in comments.
- The convolution
c[i + j] += a[i] * b[j]inO(n * m), with the result sizedn + m - 1and trimmed. - Error handling:
NULLinputs, failedmalloc, size overflow inn + m - 1, coefficient overflow. - Sparse versus dense representation and when each wins.
- Tests for zero polynomials, constants, differing degrees and negative coefficients, plus a leak check (Valgrind or AddressSanitizer).
Follow-ups
- Make it fast for large degrees (Karatsuba, or FFT/NTT).
- Add
poly_addandpoly_eval(Horner's rule). - Store sparse polynomials and multiply them efficiently.
Where it's asked
- Nvidia · Technical phone screen · Last asked Feb 2026Live technical interview, 45 to 90 minutes; a project discussion, then a small task
What it tests
c++ · api design · memory management
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
