Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

244. Dot product

easy
GeneralGeneral
senior

Compute the dot product between two vectors, a core linear algebra operation used in ML for measuring similarity and building linear models. In this task, you’ll implement it effectively using NumPy.

x⋅y=∑i=1nxiyi\mathbf{x} \cdot \mathbf{y} = \sum_{i=1}^{n} x_i y_i

Requirements

Implement the function:

python

Rules:

  • Compute the dot product defined by: xâ‹…y=∑i=1nxiyix \cdot y = \sum_{i=1}^{n} x_i y_i
  • Return a single float value.
  • You can use any valid NumPy method (e.g., np.dot, @ operator, or sum(x * y)).
  • Input vectors are NumPy arrays.

Example

python

Output:

python
Input Signature
ArgumentType
xnp.ndarray
ynp.ndarray
Output Signature
Return NameType
valuefloat

Constraints

  • Inputs are NumPy arrays; output float

  • Single function; O(n) time

Hint 1

Use np.dot(x, y) for a direct calculation.

Hint 2

Alternatively, x @ y or np.sum(x * y) achieves the same result.

Hint 3

Ensure the inputs are NumPy arrays for these operators to work efficiently.

Roles
ML Engineer
AI Engineer
Data Scientist
Quantitative Analyst
Companies
GeneralGeneral
Levels
senior
entry
Tags
linear-algebra
dot-product
numpy
iteration
10 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit