Cosine Similarity
The measure behind every embedding search and RAG system: how aligned are two vectors, regardless of their length? It is the dot product divided by both magnitudes — 1 means identical direction, 0 orthogonal, -1 opposite.
The problem
Given two equal-length vectors a and b, return their cosine similarity: dot(a, b) / (‖a‖ · ‖b‖), where ‖v‖ is the Euclidean norm (square root of the sum of squares). The result lies in [-1, 1] and ignores vector length — only direction matters.
a = [1, 2, 3], b = [1, 2, 3]1.0a = [1, 0], b = [0, 1]0.0a = [1, 0], b = [-1, 0]-1.0a = [1, 1], b = [2, 2]1.0- len(a) == len(b) ≥ 1, and neither vector is all zeros.
- Result in [-1, 1], within floating-point tolerance.
- Cosine ignores magnitude — scaling a vector does not change the answer.
Your turn — write it
Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.
Implement cosine_similarity(a, b) = dot product of a and b, divided by the product of their Euclidean norms.
- The dot product is the sum of element-wise products:
Σ aᵢ·bᵢ. - The Euclidean norm of
vissqrt(Σ vᵢ²). - Divide the dot product by
‖a‖ · ‖b‖to cancel out length and leave only direction. - Identical vectors give 1; orthogonal give 0; opposite give -1.
Approach, complexity & discussion — open after you solve
The approach
Compute the dot product of the two vectors, then divide by the product of their magnitudes: cos = (a·b) / (‖a‖‖b‖). Accumulate the dot product and both sums-of-squares in a single pass, take the square roots at the end, and guard against a zero-magnitude vector before dividing.
Complexity
Time O(d) in the dimension; space O(1) — a few running scalars, one pass.
Common mistakes
- Returning the raw dot product without dividing by the magnitudes — then length dominates and it is no longer cosine.
- Dividing by zero on a zero vector — guard it (define the similarity as 0 or handle explicitly).
- Assuming the inputs are the same length without checking.
Where this shows up
Cosine similarity is the standard score for comparing embeddings — semantic search, RAG retrieval, clustering, deduplication, recommendations. On vectors that are already unit-normalized (many embedding models return these) it reduces to a plain dot product, which is exactly why vector databases can rank by dot product and get cosine ordering for free.
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.