RAG & Retrieval·Question 8 of 100Basic

When do you need approximate nearest neighbour search instead of exact search?

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

Later than most people think. Exact (brute-force) k-NN is a matrix multiply of the query vector against all document vectors - with SIMD/GPU that's single-digit milliseconds up to roughly a million vectors. Under ~1M vectors, exact search is often the right answer: 100% recall by definition, no index build, no parameters to tune, trivial to filter. pgvector without an index, FAISS IndexFlat, or even NumPy will do.

Full worked answer

Later than most people think. Exact (brute-force) k-NN is a matrix multiply of the query vector against all document vectors - with SIMD/GPU that's single-digit milliseconds up to roughly a million vectors. Under ~1M vectors, exact search is often the right answer: 100% recall by definition, no index build, no parameters to tune, trivial to filter. pgvector without an index, FAISS IndexFlat, or even NumPy will do.

You reach for ANN when corpus size × query rate makes exact scan too slow or too expensive - typically in the millions-to-billions range or at high QPS. ANN structures (HNSW graphs, IVF partitions, quantization) trade a controlled amount of recall for orders-of-magnitude less work per query: instead of scoring every vector, you visit a tiny, well-chosen subset.

The trade is explicit and measurable: recall@k vs latency vs memory. A well-tuned HNSW gets 95-99% recall at millisecond latency, but that missing 1-5% is silent - the failure mode is that the true best chunk simply doesn't come back and nothing errors. So whenever you deploy ANN, benchmark recall against exact search on a sample of real queries, and re-check after bulk inserts or deletes (index quality degrades; some indexes need rebuilding).

Going further: follow-up territory

Two practical wrinkles worth mentioning: filters interact badly with ANN (a highly selective metadata filter can leave a pruned graph nearly unsearchable - see the pre/post-filtering question), and exact search composes trivially with filters since you can scan just the filtered subset. A common production pattern is hybrid by tenant size: exact scan for small tenants' partitions, ANN for the few giant ones.

What a good answer covers

  • Exact (brute-force) k-NN is a matrix multiply of the query vector against all document vectors - with…
  • Under ~1M vectors, exact search is often the right answer
  • You reach for ANN when corpus size × query rate makes exact scan too slow or too expensive - typically…
  • ANN structures (HNSW graphs, IVF partitions, quantization) trade a controlled amount of recall for…
  • The trade is explicit and measurable: recall@k vs latency vs memory

Key terms: ANN · recall@k vs latency vs memory

Interviewer follow-ups to expect

  1. How exactly would you measure the recall your ANN index achieves?

  2. Your p99 latency doubled after deleting 30% of vectors - what happened?

  3. At what point does the ANN index's RAM cost push you to quantization?

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