Compute the matrix product of two NumPy arrays with the @ operator

Quick Overview

Write a NumPy function that returns the matrix product of two 2-D arrays using the @ operator. It tests whether you know how the matrix product differs from elementwise multiplication, the inner-dimension rule for shapes, and what the operation costs.

Compute the matrix product of two NumPy arrays with the @ operator

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write a function `matmul(A, B)` that returns the matrix product of two 2-D NumPy arrays using the `@` operator (or `np.matmul`). ```python A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) matmul(A, B) # array([[19, 22], # [43, 50]]) ``` ```hint Check one entry by hand Work out entry (0, 0) of the expected output from the inputs. It sums products along a dimension the two arrays share, which elementwise multiplication never does. ``` ### Constraints & Assumptions - `A` and `B` are 2-D NumPy arrays. `A` has shape `(m, k)` and `B` has shape `(k, n)`. - The result has shape `(A.shape[0], B.shape[1])`. - Do not use elementwise `*` or `np.multiply`. ### What a Strong Answer Covers - Computes the product with the matrix product operator and returns an array of shape `(m, n)`. - Explains how elementwise `*` differs, and why square inputs make that mistake easy to miss. - States the inner-dimension rule and what happens when the shapes break it. - Gives the time and space cost of the product. ### Follow-up Questions - How does `A * B` differ from `A @ B`? - What happens if the inner dimensions don't match, and how would you report it to the caller? - How does `@` behave when one operand is 1-D, or when the inputs are 3-D stacks of matrices? - How does `np.dot` differ from `np.matmul` for arrays with more than two dimensions?

Overview: Write a NumPy function that returns the matrix product of two 2-D arrays using the @ operator. It tests whether you know how the matrix product differs from elementwise multiplication, the inner-dimension rule for shapes, and what the operation costs.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Write a function matmul(A, B) that returns the matrix product of two 2-D NumPy arrays using the @ operator (or np.matmul).

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
matmul(A, B)
# array([[19, 22],
#        [43, 50]])

Constraints & Assumptions

  • A and B are 2-D NumPy arrays. A has shape (m, k) and B has shape (k, n) .
  • The result has shape (A.shape[0], B.shape[1]) .
  • Do not use elementwise * or np.multiply .

What a Strong Answer Covers Guidance

  • Computes the product with the matrix product operator and returns an array of shape (m, n) .
  • Explains how elementwise * differs, and why square inputs make that mistake easy to miss.
  • States the inner-dimension rule and what happens when the shapes break it.
  • Gives the time and space cost of the product.

Follow-up Questions Guidance

  • How does A * B differ from A @ B ?
  • What happens if the inner dimensions don't match, and how would you report it to the caller?
  • How does @ behave when one operand is 1-D, or when the inputs are 3-D stacks of matrices?
  • How does np.dot differ from np.matmul for arrays with more than two dimensions?