FastPrepMatrix Multiplication With and Without NumPy

Matrix Multiplication With and Without NumPy

NVIDIA logoNVIDIA● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Given two rectangular integer matrices a with shape R × K and b with shape K × C, return their matrix product as a new R × C matrix.

Each output cell is the dot product of one row of a and one column of b:

product[row][column] = sum(a[row][inner] * b[inner][column], inner = 0..K-1)
  • Keep rows and columns in their original order.
  • Use exact integer multiplication and addition. Products and sums may exceed signed 32-bit range, so use signed 64-bit arithmetic where required.
  • Do not modify either input matrix.
  • All inputs have compatible dimensions and satisfy the stated constraints. Empty matrices, ragged rows, incompatible shapes and floating-point values are outside this exercise.

Two implementations

Implement the product manually without a matrix library. Also write the equivalent Python expression using NumPy for the same input and output contract. Both implementations must compute matrix multiplication, rather than elementwise multiplication. The callable submission returns the product; the editorial includes the NumPy comparison.

What the interview report shared

The report asks for matrix multiplication with and without NumPy. The candidate describes using the row-by-column relationship and running the code successfully. The report does not reproduce its example or specify the matrix shapes, numeric bounds or error behavior.

Function

multiplyMatrices(a: int[][], b: int[][]) → long[][]

Examples

Example 1

a = [[1,2,3],[4,5,6]]b = [[7,8],[9,10],[11,12]]return = [[58,64],[139,154]]

The first cell is 1 * 7 + 2 * 9 + 3 * 11 = 58. The two rows of a and two columns of b produce a 2 × 2 matrix.

Example 2

a = [[-1000000]]b = [[1000000]]return = [[-1000000000000]]

The single product is -10^6 * 10^6 = -10^12. Its magnitude exceeds a signed 32-bit integer.

Example 3

a = [[2],[-3]]b = [[4,0,-5]]return = [[8,0,-10],[-12,0,15]]

The shared dimension is 1. Each row of the product is one entry from a multiplied by the row [4,0,-5] from b.

Constraints

  • 1 <= R, K, C <= 30.
  • a.length = R, every row of a has K entries, b.length = K, and every row of b has C entries.
  • -10^6 <= a[row][inner], b[inner][column] <= 10^6.
  • Every individual product, partial sum and result fits a signed 64-bit integer; the absolute value of any partial sum is at most 30 * 10^12.

More NVIDIA problems

See NVIDIA hiring insights
public long[][] multiplyMatrices(int[][] a, int[][] b) {
    // Write your code here.
}
a[[1,2,3],[4,5,6]]
b[[7,8],[9,10],[11,12]]
expected[[58,64],[139,154]]
Checking account…