LearnThatStack Ace your next interview
Embeddings & Vector Databases · question
Question 3 of 56

Compare cosine similarity, dot product, and Euclidean distance. When do they produce the same rankings?

beginner
← All Embeddings & Vector Databases questions
Re-explain

All three rank by closeness, but they differ in how they treat a vector's magnitude, its length. Cosine similarity looks only at the angle, dot product also rewards longer vectors, and Euclidean distance measures the straight-line gap.

Metric Formula Uses length? Best score
Cosine a·b / (|a| |b|) no highest
Dot product a·b yes highest
Euclidean (L2) |a - b| yes lowest

They produce the same ranking when every stored vector has the same norm, which in practice means embeddings normalized to length 1. Then cosine equals the dot product, and this identity ties in the third:

|a - b|^2 = 2 - 2 * (a·b)    # for unit vectors

Distance falls as the dot product rises, so the order matches.

Once the stored norms match, the query's own norm never changes the order, because it scales every candidate's dot product equally. If document norms vary, dot product can push a long, loosely related vector above a short, on-topic one.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Embeddings & Vector Databases One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Show why Euclidean distance on unit vectors ranks results the same way cosine does. Expand |a - b|^2 into |a|^2 + |b|^2 - 2(a·b). With both norms at 1 it becomes 2 - 2(a·b), which falls as the dot product rises.
  • Why doesn't the query's own norm change a dot-product ranking, when document norms do? The query norm multiplies every candidate's score by the same factor. Document norms differ per candidate, so they can reorder results.
  • Why do many systems normalize vectors at write time and then search with inner product? Once vectors are normalized at write time, dot product equals cosine, so no comparison has to divide by the two norms.
  • How does the way an embedding model was trained decide which metric you should use? Use the metric the model was trained with. Normalizing can drop length signal a dot-product model learned. Raw dot product lets length skew a cosine model.
  • Is a cosine score of 0.8 a reliable sign of relevance? Would you set a fixed cutoff? Score ranges shift with the model, domain and query. Calibrate any cutoff per model on labeled data, or rely on rank and a reranker.

What you can say

  1. All three score how close two vectors are, and the real difference is how each one treats a vector's length.
  2. Cosine looks only at the angle, dot product also rewards longer vectors, and Euclidean measures the straight-line gap, where the lowest score wins.
  3. They produce the same ranking when every stored vector has the same norm, which in practice means embeddings normalized to length 1.
  4. Then cosine equals the dot product, and for unit vectors the squared distance is 2 minus 2 times the dot product, so distance falls as similarity rises.
  5. The query's own norm doesn't change the order, because it scales every candidate's dot product by the same amount.
  6. The catch is that with unequal document norms, dot product can rank a long, loosely related vector above a short, on-topic one.

Weak answers to avoid

  • Lists the formulas but skips when they agree The second half is the real test. Say they rank the same when stored vectors share a norm, usually length 1, and tie it together with |a - b|^2 = 2 - 2(a·b).
  • Says cosine and dot product always match They match only when norms are equal. With varying document norms, dot product can rank a long, loosely related vector above a short, on-topic one.
  • Forgets Euclidean ranks lowest-first Euclidean is a distance, so the smallest value is the best match. Say that for unit vectors distance falls as dot product rises, so the order still matches.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Embeddings & Vector Databases cheatsheet.

← Back to all Embeddings & Vector Databases questions
Pro · $10/mo

49 of 56 Embeddings & Vector Databases answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime