Problems
Loading...
1 / 1

Implement Dot Product

Linear Algebra
Easy

The dot product of two equal-length 1-D arrays xx and yy of length nn is defined algebraically as:

xy=i=1nxiyi=x1y1+x2y2++xnynx \cdot y = \sum_{i=1}^{n} x_i y_i = x_1 y_1 + x_2 y_2 + \cdots + x_n y_n

and geometrically as:

xy=xycos(θ)x \cdot y = \|x\| \cdot \|y\| \cdot \cos(\theta)

where x\|x\| and y\|y\| are the magnitudes of xx and yy, and θ\theta is the angle between them.

Return the dot product as a scalar float.

Loading visualization...

Examples

Input: x = [1,2,3], y = [4,5,6]

Output: 32.0

1×4 + 2×5 + 3×6 = 4 + 10 + 18 = 32

Input: x = [1,0], y = [0,1]

Output: 0.0

Orthogonal vectors (perpendicular)

Input: x = [-1,2], y = [3,-1]

Output: -5.0

(-1)×3 + 2×(-1) = -3 + (-2) = -5

Hint 1

Convert inputs to NumPy arrays first, then use vectorized operations.

Hint 2

NumPy has a built-in function for this: np.dot(x, y)\texttt{np.dot(x, y)}.

Requirements

  • Must work for lists or NumPy arrays
  • Must return a float
  • Must be vectorized (no Python element loops)
  • Must handle 1D arrays only
  • Must raise ValueError for mismatched lengths

Constraints

  • Time limit: 200 ms, Memory: 64 MB
  • NumPy only (no sklearn, scipy)
Try Similar Problems