Mockbit/#70
ML CodingmediumNumpy~15m

Cosine Similarity

Problem

You are building a semantic search engine that compares document embeddings. To find how similar two documents are, you compute the cosine similarity between their embedding vectors — a value between -1 and 1 where 1 means identical direction, 0 means orthogonal, and -1 means opposite.

Given two 1D vectors a and b of equal length, return their cosine similarity:

cosine_similarity(a, b) = dot(a, b) / (norm(a) * norm(b))

If either vector is all zeros (no magnitude), return 0.0. Round the result to 6 decimal places.

Examples

Example 1

Input: ([1, 2, 3], [100, 200, 300])
Output: 1.0

b is just a scaled version of a — same direction. Cosine similarity measures angle not magnitude, so scaling has no effect.

Example 2

Input: ([1, 2, 3], [-1, -2, -3])
Output: -1.0

Opposite direction vectors. dot product is negative, norms are equal, result is -1.0.

Example 3

Input: ([1, 1], [1, 0])
Output: 0.707107

45-degree angle between vectors. dot(a,b)=1, norm(a)=√2, norm(b)=1. Result: 1/√2 ≈ 0.707107.

Constraints
  • 1 <= len(a) == len(b) <= 1000
  • -1000 <= a[i], b[i] <= 1000
Reference solution

Reference solution available after you attempt the question.

Ready to solve it?

Start a session on Mockbit #70. Write your code, run it against hidden tests, and get graded with specific critique on each axis.

Related ML Coding questions
← Back homemockbit.io/q/70
PrivacyTerms© 2026 Mockbit