Interview Q&A / AI & Generative AI

IntermediateIn-depth3 min readUpdated 2 Oct 2026

What are embeddings, and how is vector similarity used to compare text?

SHORT ANSWER

An embedding is a list of numbers that represents the meaning of a piece of text, so texts with similar meaning get vectors that point in similar directions. Similarity measures such as cosine similarity compare these vectors to find related text.

Explain it simply

Imagine giving every book in a college library a location on a giant map, where books on similar subjects are placed close together. Data structures books sit near algorithms books, and cookbooks sit far away in another corner. To find books related to the one in your hand, you just look at its neighbours on the map.

An embedding is that location, written as a list of numbers (a vector). An embedding model reads a sentence and outputs, say, a few hundred or a few thousand numbers. Sentences that mean similar things land close together, even if they use different words. "How do I reset my net banking password?" and "I forgot my login PIN for online banking" share few words, but their embeddings are close.

Example

Real embeddings have hundreds of dimensions. Here is a tiny three-dimensional version to show the idea:

js
function cosine(a, b) {
  let dot = 0, normA = 0, normB = 0;
  for (let i = 0; i < a.length; i++) {
    dot += a[i] * b[i];
    normA += a[i] * a[i];
    normB += b[i] * b[i];
  }
  return (dot / (Math.sqrt(normA) * Math.sqrt(normB))).toFixed(2);
}

const cricket = [0.9, 0.1, 0.2];
const football = [0.8, 0.2, 0.1];
const biryani = [0.1, 0.9, 0.3];

console.log(cosine(cricket, football)); // Output: 0.99
console.log(cosine(cricket, biryani)); // Output: 0.27

"Cricket" and "football" are both sports, so their vectors point in almost the same direction. "Biryani" points elsewhere.

How it works

  1. An embedding model, a neural network trained so that related texts get nearby vectors, converts each text into a fixed-length vector.
  2. You embed all your documents once and store the vectors, often in a vector database or a vector index.
  3. At query time you embed the user's question with the same model.
  4. You find the stored vectors closest to the query vector.

Cosine similarity measures the angle between two vectors: close to 1 means same direction, around 0 means unrelated. Dot product and Euclidean distance are also common. Many embedding models return normalised vectors (length 1), and then cosine similarity and dot product give the same ranking.

Comparing a query with millions of vectors one by one is slow, so vector databases use approximate nearest neighbour (ANN) indexes that find very close matches much faster, at the cost of occasionally missing the exact best one.

Real-world use

  • Semantic search over FAQs or policy documents, finding answers even when the wording differs.
  • The retrieval step in RAG, where the closest chunks are given to an LLM as context.
  • Recommending similar products, articles or job postings.
  • Grouping similar support tickets or detecting near-duplicate questions.

Common mistakes

  • Comparing vectors made by two different embedding models. Their spaces are unrelated, so the scores mean nothing.
  • Treating similarity as correctness. A close match is related, not necessarily the right answer.
  • Embedding whole long documents as one vector. The meaning gets blurred; splitting into smaller chunks usually retrieves better.

What to say in the interview

"An embedding is a dense vector that captures the meaning of text, produced by a model trained so that similar texts get nearby vectors. To compare texts I embed both with the same model and use a similarity measure, usually cosine similarity, which looks at the angle between vectors. In practice we embed documents ahead of time, store them in a vector index, embed the query at runtime, and fetch the nearest neighbours. That is the basis of semantic search and the retrieval step in RAG."

Follow-up questions

  • What is the difference between cosine similarity and Euclidean distance?
  • Why do vector databases use approximate nearest neighbour search?
  • How would you choose the chunk size when embedding long documents?

#embeddings#vectors#cosine-similarity#semantic-search#generative-ai

Related questions

Practise more AI & Generative AI questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open AI & Generative AI quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.