AI EngineeringInterview preparation
Foundation·Concept·2 min read

Similarity and cosine similarity

Practice this question

Overview

  • Similarity measures how close two vectors are according to a selected metric.
  • Cosine similarity compares their direction, effectively asking whether they point in a similar direction regardless of magnitude.
  • Dot product and Euclidean distance are other common metrics.

Why this matters

A retrieval engine needs a numerical way to order candidate documents.

An everyday example

Two arrows pointing northeast are directionally similar even if one is longer.

A technical example

For nonzero vectors, cosine(a,b) = dot(a,b)/(length(a)*length(b)). The Python example in section 2 implements it.

How it works

  1. Encode query
  2. Calculate or approximate distances to indexed vectors
  3. Sort by score
  4. Return the top candidates after required filtering.

Architecture and flow

Query vector + document vectors → selected metric → ranked candidates.

The insurance assistant example

Compare a policy question with policy chunks, then verify whether the retrieved clauses actually answer it.

When to use it

When the metric matches the embedding model's recommendation and the index configuration.

When to avoid it

Do not reuse a score cutoff from another model or treat 0.9 as “90% probability of correctness.”

Interview answer

“Cosine compares vector directions. Its score helps rank candidates, but relevance thresholds must be calibrated on our own embedding model and data.”

Benefits, trade-offs, and production considerations

  • Cosine ranges from -1 to 1 mathematically; database scores may be transformed into another range.
  • Normalized vectors can make dot product equivalent for ranking.
  • Reject zero or invalid vectors and do not mix incompatible model spaces.
Adapted from 13_AI.MD · “Similarity and cosine similarity”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.