Multiply Two Integer Matrices of Compatible Dimensions

Read the full interview experience this question came from →

Quick Overview

Multiply two integer matrices with compatible dimensions and return their product. This coding warm-up tests careful indexing over rows, columns and the shared inner dimension, correct output dimensions for non-square inputs, and clean handling of single-row and single-column matrices.

Multiply Two Integer Matrices of Compatible Dimensions

Company: eBay

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given two integer matrices `a` and `b`, return their matrix product. ### Function Signature ```python def multiply_matrices(a: list[list[int]], b: list[list[int]]) -> list[list[int]]: ``` ### Rules - `a` has `m` rows and `n` columns, and `b` has `n` rows and `p` columns. Every row of a matrix has the same length. - The number of columns of `a` always equals the number of rows of `b`, so the product is always defined. - Return the product `c` as a list of `m` rows, each a list of `p` integers, where `c[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + ... + a[i][n-1] * b[n-1][j]`. ### Constraints - `1 <= m, n, p <= 100` - `-1000 <= a[i][j] <= 1000` and `-1000 <= b[i][j] <= 1000` - Every entry of the result has absolute value at most `100 * 1000 * 1000 = 10^8`, so it fits in a 32-bit signed integer. ### Examples **Example 1** ```text Input: a = [[1, 2], [3, 4]], b = [[5, 6], [7, 8]] Output: [[19, 22], [43, 50]] ``` For example, `c[0][0] = 1 * 5 + 2 * 7 = 19` and `c[1][1] = 3 * 6 + 4 * 8 = 50`. **Example 2** ```text Input: a = [[1, 0, -2]], b = [[3], [4], [5]] Output: [[-7]] ``` A 1 × 3 matrix times a 3 × 1 matrix gives a 1 × 1 matrix: `1 * 3 + 0 * 4 + (-2) * 5 = -7`. **Example 3** ```text Input: a = [[2], [-1]], b = [[1, 0, 3]] Output: [[2, 0, 6], [-1, 0, -3]] ``` A 2 × 1 matrix times a 1 × 3 matrix gives a 2 × 3 matrix.

Overview: Multiply two integer matrices with compatible dimensions and return their product. This coding warm-up tests careful indexing over rows, columns and the shared inner dimension, correct output dimensions for non-square inputs, and clean handling of single-row and single-column matrices.

Read the full eBay Machine Learning Engineer interview experience this question came from

eBay logo
eBay
Oct 24, 2026
mediumMachine Learning EngineerOnsiteCoding & Algorithms
0
0

Given two integer matrices a and b, return their matrix product.

Function Signature

def multiply_matrices(a: list[list[int]], b: list[list[int]]) -> list[list[int]]:

Rules

  • a has m rows and n columns, and b has n rows and p columns. Every row of a matrix has the same length.
  • The number of columns of a always equals the number of rows of b , so the product is always defined.
  • Return the product c as a list of m rows, each a list of p integers, where c[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + ... + a[i][n-1] * b[n-1][j] .

Constraints

  • 1 <= m, n, p <= 100
  • -1000 <= a[i][j] <= 1000 and -1000 <= b[i][j] <= 1000
  • Every entry of the result has absolute value at most 100 * 1000 * 1000 = 10^8 , so it fits in a 32-bit signed integer.

Examples

Example 1

Input:  a = [[1, 2], [3, 4]], b = [[5, 6], [7, 8]]
Output: [[19, 22], [43, 50]]

For example, c[0][0] = 1 * 5 + 2 * 7 = 19 and c[1][1] = 3 * 6 + 4 * 8 = 50.

Example 2

Input:  a = [[1, 0, -2]], b = [[3], [4], [5]]
Output: [[-7]]

A 1 × 3 matrix times a 3 × 1 matrix gives a 1 × 1 matrix: 1 * 3 + 0 * 4 + (-2) * 5 = -7.

Example 3

Input:  a = [[2], [-1]], b = [[1, 0, 3]]
Output: [[2, 0, 6], [-1, 0, -3]]

A 2 × 1 matrix times a 1 × 3 matrix gives a 2 × 3 matrix.