Build your own vector database
Building the index (Algorithm 1)·30 min

Implement INSERT

0 / 1
# grade this stage (from your course directory)
npx theinference test --stage insert-full --watch

Compose everything into the full insert, then buildIndex to insert the whole corpus in the fixed order. This is the moment a real HNSW graph comes into existence — and you grade it on its structure, not yet on search.

What to build

Your task

Implement INSERT + buildIndex

Implement HNSW.insert(id, level) (the Algorithm 1 loop using your search-layer, select-heuristic, and connect) and buildIndex(vs, params) (assign all levels, insert 0..n-1). Wire the build-structure subcommand in src/cli.ts to build and print structural invariants.

your_program.sh
./your_program.sh build-structure <fixture>/chapters/04-insert/steps/03-insert-full/fixtures/data.json
What build-structure prints
{ "topLayer": L, "maxDeg0": <= Mmax0, "maxDegUp": <= Mmax,
  "degreeCapsOk": 1, "layer0Connected": 1, "layerCounts": { "l0": ... } }
How it's graded

Process mode: stdout_json_match against the frozen reference. The checks: every degree within its cap (Mmax0 at layer 0, Mmax above), layer 0 fully connected (BFS from the entry point reaches all nodes), and the per-layer counts match. Because the build is deterministic, your structure equals the reference exactly.

If layer 0 is not fully connected

Your shrink step is probably dropping the only link to a node. Re-selecting with the heuristic and keeping pruned connections — which tops a shrunken neighborhood back up to capacity rather than discarding the surplus outright — is what preserves connectivity. That is the exact behavior the connect step asked you to build.