Senior

HNSW vs IVF: how do vector indexes trade off recall, speed and memory?

How to answer it in an AI engineer interview: what is being tested, a step-by-step framework, a sample answer and the mistakes to avoid.

$19.99 on Gumroad, instant download40% off with code EARLYBF until Nov 3, 11:59 PM PTEARLYBF: 40% off until Nov 3

Smaller start: Score your answers against 50 rubrics, $9. 50 AI engineer questions, one rubric each, PDF on Gumroad.

Practiced one out loud? Paste your answer into the free self-check and get a score in 30 seconds

What the interviewer is testing

Whether you understand approximate nearest-neighbor search well enough to pick an index for a workload and tune it with measurements.

Answer framework

  1. Both are approximate nearest-neighbor (ANN) indexes: they trade a little recall for much faster search than exact comparison.
  2. HNSW: a layered proximity graph searched greedily from coarse to fine; high recall and low latency, but high memory and slower builds.
  3. IVF: cluster vectors into lists and search only the nearest few clusters; lower memory, tuned by the number of probes, often combined with product quantization (PQ).
  4. Tune: HNSW with M and efSearch, IVF with the number of lists and nprobe.
  5. Measure recall@k against exact search on a sample, plus latency and memory.

Sample answer

Exact nearest-neighbor search compares the query with every vector, which is too slow at scale, so we use approximate indexes.

HNSW builds a multi-layer graph where each vector links to its near neighbors. A search starts in a sparse top layer and walks greedily toward the query, dropping to denser layers. It gives excellent recall at low latency and handles inserts well, but the graph links add a lot of memory on top of the vectors, and builds are slower. Raising efSearch improves recall at the cost of latency.

IVF first clusters the vectors, for example into a few thousand lists, and at query time searches only the nprobe closest lists. It uses less memory, and with product quantization the vectors themselves are compressed, which lets billions of vectors fit. The cost is lower recall when the answer sits in a cluster you did not probe, and clusters need retraining as data shifts.

I would use HNSW when the index fits in memory and latency matters most, and IVF-PQ when memory or scale is the constraint. Either way I measure recall@10 against exact search on a sample of queries before choosing parameters.

Likely follow-ups

What happens to recall if you lower nprobe in IVF? Search gets faster but recall falls, because fewer clusters are searched.

Common mistakes

The PlaybookWant the full method?

The 0 to 1 AI Engineer Interview Playbook covers the answer frameworks, system-design walkthroughs and an 8-week plan.

Get the playbook 40% off with code EARLYBF until Nov 3, 11:59 PM PTEARLYBF: 40% off until Nov 3

Smaller start: Score your answers against 50 rubrics, $9. 50 AI engineer questions, one rubric each, PDF on Gumroad.

Interviewing for PM or TPM? See the full PM package, $59 on Gumroad: a resume system, a PM, TPM and PGM interview handbook, a 1:1 cheat sheet and a PM playbook. PM and TPM content, not AI engineer prep.

Related questions

Based on question #36 in the full question bank.