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

Explain k-nearest-neighbor search and the difference between exact and approximate nearest neighbor search.

beginner
← All Embeddings & Vector Databases questions
Re-explain

k-nearest-neighbor search, or k-NN, takes a query vector and returns the k stored vectors closest to it under a distance metric. It ranks by closeness, it does not look for an exact match.

Exact search compares the query against every stored vector. It always returns the true top k, but the work grows linearly with the collection size.

scores = corpus @ query          # touches every vector, every query
top_k = np.argsort(-scores)[:k]

Approximate nearest neighbor (ANN) search skips most of the collection by using an index built ahead of time. It is much faster, but it can miss some true neighbors.

Aspect Exact Approximate
Result always the true top k usually close to it
Query cost scales with collection size roughly sub-linear
Extra memory none index structure
Tuning nothing to tune speed versus recall

Exact search is fine for small collections. Once you reach millions of vectors, ANN is the practical default.

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

What you can say

  1. k-nearest-neighbor search takes a query vector and returns the k stored vectors closest to it under a distance metric, so it ranks by closeness rather than looking for an exact match.
  2. Exact search compares the query against every stored vector, so it always returns the true top k, but the cost grows linearly with the size of the collection.
  3. In code that's one dot product of the query against the whole corpus and then a sort, and it touches every vector on every query.
  4. Approximate nearest neighbor search builds an index ahead of time, so each query only looks at a small part of the collection and the cost is roughly sub-linear.
  5. The price is extra memory for the index and some missed true neighbors, so you end up tuning speed against recall.
  6. I'd use exact search for small collections, but once you're at millions of vectors, ANN is the practical default.

Weak answers to avoid

  • Treats k-NN as an exact-match lookup k-NN ranks by distance and returns k results even if none are close. Say it returns the k closest vectors under a metric, not matches.
  • Confuses k-NN search with the k-NN classifier The classifier votes on neighbor labels. This question is about retrieval, so talk about returning the top k vectors and what each query costs.
  • Says ANN is just faster, with no downside ANN can miss true neighbors and needs memory for its index. Name recall as the price and say you tune speed against it.
  • Can't say when exact search is good enough An index on a small collection adds memory and tuning for nothing. Say brute force is fine there and ANN becomes the default at millions of vectors.
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