TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement Dot Product

Linear Algebra
Easy

The dot product of two equal-length 1-D arrays xxx and yyy of length nnn is defined algebraically as:

x⋅y=∑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_nx⋅y=i=1∑n​xi​yi​=x1​y1​+x2​y2​+⋯+xn​yn​

and geometrically as:

x⋅y=∥x∥⋅∥y∥⋅cos⁡(θ)x \cdot y = \|x\| \cdot \|y\| \cdot \cos(\theta)x⋅y=∥x∥⋅∥y∥⋅cos(θ)

where ∥x∥\|x\|∥x∥ and ∥y∥\|y\|∥y∥ are the magnitudes of xxx and yyy, 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)}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
Cosine SimilarityEuclidean DistanceMatrix TransposeMatrix TraceManhattan Distance

Sign in to take notes on this problem

Case 1
Case 2
Case 3
Case 4

Accepts: array

Accepts: array

You must run your code first.
PrevNext

Implement Dot Product

Linear Algebra
Easy

The dot product of two equal-length 1-D arrays xxx and yyy of length nnn is defined algebraically as:

x⋅y=∑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_nx⋅y=i=1∑n​xi​yi​=x1​y1​+x2​y2​+⋯+xn​yn​

and geometrically as:

x⋅y=∥x∥⋅∥y∥⋅cos⁡(θ)x \cdot y = \|x\| \cdot \|y\| \cdot \cos(\theta)x⋅y=∥x∥⋅∥y∥⋅cos(θ)

where ∥x∥\|x\|∥x∥ and ∥y∥\|y\|∥y∥ are the magnitudes of xxx and yyy, 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)}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
Cosine SimilarityEuclidean DistanceMatrix TransposeMatrix TraceManhattan Distance

Sign in to take notes on this problem

Case 1
Case 2
Case 3
Case 4

Accepts: array

Accepts: array

You must run your code first.