ML & Deep Learning Foundations·Question 9 of 55Basic

What are embeddings? Compare cosine similarity, dot product, and Euclidean distance - when does the choice matter?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

An embedding is a learned dense vector (typically 256-4096 dims) where geometry encodes semantics: similar items map to nearby points. They're produced by an encoder trained so that a chosen similarity function scores related pairs highly - words, sentences, images, users, code.

Full worked answer with diagram

An embedding is a learned dense vector (typically 256-4096 dims) where geometry encodes semantics: similar items map to nearby points. They're produced by an encoder trained so that a chosen similarity function scores related pairs highly - words, sentences, images, users, code.

The three similarity measures:

  • Dot product a·b - rewards both alignment and magnitude. A long vector can dominate rankings regardless of direction.
  • Cosine = a·b / (‖a‖‖b‖) - direction only; equivalent to dot product on unit-normalized vectors.
  • Euclidean ‖a−b‖ - for unit vectors, ‖a−b‖² = 2 − 2·cos(a,b), so it's monotonically equivalent to cosine and produces identical rankings.

So on normalized vectors all three agree on ranking; the choice only matters when magnitudes carry information. The senior-engineer answer: use the similarity the model was trained with. Most modern text embedders (OpenAI text-embedding-3, Cohere, E5/GTE/BGE-style open models) are trained with cosine-based contrastive objectives and often ship pre-normalized vectors. Some retrieval models deliberately use raw dot product so magnitude can encode confidence or importance.

Operationally, your vector index metric must match: FAISS/pgvector/HNSW indexes are built for inner product, L2, or cosine, and a mismatch silently degrades retrieval quality. Two more advanced points worth volunteering: raw LLM hidden states are anisotropic (squashed into a narrow cone), which is why you use a contrastively trained embedder rather than pooling base-model activations; and Matryoshka (MRL) embeddings are trained so truncating to the first k dims still works, letting you trade recall for memory/latency.

Worth sketching. The last box is the part people forget, and drawing it shows you have actually shipped a vector index.

flowchart TD
    A["Choose a similarity"] --> B{"Vectors unit-normalized?"}
    B -->|"yes"| C["Cosine, dot and L2<br/>all give the same ranking"]
    B -->|"no"| D{"Does magnitude<br/>carry information?"}
    D -->|"yes, trained on raw dot"| E["Dot product"]
    D -->|"no"| F["Normalize, then cosine"]
    C --> G["ANN index metric must match<br/>the training objective"]
    E --> G
    F --> G

What a good answer covers

  • An embedding is a learned dense vector (typically 256-4096 dims) where geometry encodes semantics
  • They're produced by an encoder trained so that a chosen similarity function scores related pairs highly…
  • The three similarity measures: - Dot product a·b - rewards both alignment and magnitude
  • A long vector can dominate rankings regardless of direction
  • So on normalized vectors all three agree on ranking; the choice only matters when magnitudes carry…

Key terms: Dot product · Cosine · Euclidean · anisotropic · Matryoshka (MRL) embeddings

Interviewer follow-ups to expect

  1. Why does normalizing make Euclidean and cosine equivalent - show it. When would dot product beat cosine in a recommender?

  2. What breaks if you index cosine-trained embeddings with an L2 metric without normalizing?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep