Build your own vector database
Choosing neighbors (Algorithms 3 & 4)·15 min

SELECT-NEIGHBORS-SIMPLE

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

When inserting a new element, SEARCH-LAYER hands you a pool of candidate neighbors. Which M of them become actual edges? Algorithm 3 is the obvious answer: keep the M closest to the new element. Build it first — it is the baseline the heuristic improves on.

What to build

Your task

Implement runSelectSimple

Implement selectSimple(q, candidates, M) in src/hnsw.ts and expose runSelectSimple. Each candidate is { id, vec }; return the ids of the M nearest to q, nearest-first, ties broken by ascending id.

tests/adapters.ts
type Cand = { id: number; vec: number[] };

export function runSelectSimple(q: number[], candidates: Cand[], M: number): number[] {
  // sort candidates by L2 to q (ascending), break ties by id, take first M
}
How it's graded

Adapter mode: your returned id list is compared exactly to the reference for a fixed clustered candidate set. The same fixture drives the heuristic step two pages from now — where your answers must differ.