Explain how HNSW works, and what the M and ef parameters control.
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
HNSW - Hierarchical Navigable Small World (Malkov & Yashunin) - is a graph-based ANN index and the default in most vector stores (pgvector, Qdrant, Weaviate, Milvus, FAISS all offer it).
Full worked answer with diagram
HNSW - Hierarchical Navigable Small World (Malkov & Yashunin) - is a graph-based ANN index and the default in most vector stores (pgvector, Qdrant, Weaviate, Milvus, FAISS all offer it).
Structure: a stack of graph layers. The bottom layer (layer 0) contains every vector, each connected to its near neighbours. Each layer above contains an exponentially thinner random sample of nodes with longer-range links - think express lanes. Search starts at an entry point in the sparse top layer, greedily walks toward the query (repeatedly hopping to whichever neighbour is closest to the query vector), drops down a layer when it can't improve, and repeats until it's refining among true near neighbours at layer 0. The hierarchy gives you coarse-to-fine navigation in roughly logarithmic hops instead of a linear scan.
Parameters:
- M - max edges per node per layer. Higher M = denser graph = better recall and robustness (more routes around local minima) at the cost of memory (each edge is stored) and slower inserts. Typical 16-64.
- ef_construction - beam width while building: how many candidates are tracked when choosing each new node's neighbours. Higher = better graph quality, slower ingest. Typical 100-500.
- ef_search - beam width at query time: the search keeps the ef best candidates found so far rather than pure single-path greed. This is your main runtime knob - raise it for recall, lower it for latency, tune per query class without rebuilding. Must be ≥ k; typical 50-500.
Going further: follow-up territory
Operational facts worth volunteering: HNSW lives in RAM (memory ≈ vectors + M edges per node - often the binding constraint); inserts are incremental (no training phase, unlike IVF); deletes are the weak spot - usually tombstoned, degrading the graph until a rebuild/compaction; and recall should be measured empirically against exact search, because a greedy graph search can get stuck in local minima, which is exactly what higher M and ef mitigate.
Worth sketching. The drop-a-layer step is what people leave out when they describe HNSW as "just a graph", and it is where the log-time behaviour comes from.
flowchart TD
E["Entry point, top layer:<br/>sparse, long hops"] --> G1["Greedy hop to the<br/>closest neighbour"]
G1 -->|"no neighbour is closer"| D1["Drop one layer"]
D1 --> G2["Repeat on a<br/>denser layer"]
G2 -->|"no neighbour is closer"| L0["Layer 0: every vector,<br/>M edges each"]
L0 --> B["Keep the ef_search best<br/>candidates, return top k"]
What a good answer covers
- HNSW - Hierarchical Navigable Small World (Malkov & Yashunin) - is a graph-based ANN index and the…
- Structure: a stack of graph layers
- The bottom layer (layer 0) contains every vector, each connected to its near neighbours
- Each layer above contains an exponentially thinner random sample of nodes with longer-range links -…
- Search starts at an entry point in the sparse top layer, greedily walks toward the query (repeatedly…
Key terms: every · greedily walks · M · ef_construction · ef_search
In the glossary: HNSW · ANN · IVF · Compaction
Interviewer follow-ups to expect
Why does a highly selective metadata filter hurt HNSW specifically?
What happens to search quality after deleting 40% of vectors?
How would you tune M/ef differently for a 99%-recall offline job vs a 20ms-p99 online API?
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