Build your own vector database
The two structures HNSW rests on·18 min

Build the min-heap

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

Build the min-heap that will hold the candidate queue C. A binary heap stored in an array: push adds and sifts up, pop removes the root and sifts down. The root is always the entry with the smallest distance.

What to build

Your task

Implement MinHeap

Implement MinHeap in src/heap.ts — push(e), pop() (smallest distance), peek(), size() — over Entry { dist, id }. The harness drives it through runMinHeap(ops), which replays a push/pop op log and returns the sequence of popped ids.

tests/adapters.ts
type HeapOp = { op: "push"; dist: number; id: number } | { op: "pop" };

export function runMinHeap(ops: HeapOp[]): number[] {
  // replay ops on a MinHeap; return the id of each popped Entry, in order
}
Tie-break on ascending id

When two entries have equal distance, treat the one with the smaller id as having higher priority (it pops first). This makes two correct heaps pop the same order — which the downstream search and select steps depend on for determinism.

How it's graded

Adapter mode: the popped-id sequence is compared exactly (atol 0) to the reference for a fixed op log. A wrong sift direction or a missing tie-break will reorder the pops and fail.