Build your own vector database
Setup & the O(N) wall·9 min

The brute-force baseline

0 / 1
# grade this stage (from your course directory)
npx theinference test --stage brute-force-baseline --watch

Before you beat brute force, build it. bruteForceTopK scans every vector, sorts by distance, and returns the top-k indices. It is the baseline the whole course beats — and the recall denominator: the exact answer HNSW is graded against.

It is also our enemy: O(N·d) per query. Watch the comparison count — it touches all N vectors. HNSW's entire job is to return the same top-k while touching a tiny fraction of them.

What to build

Your task

Implement runBruteForce

Implement bruteForceTopK(query, base, k) — compute L2 from query to every row of base, return the k indices of the nearest, in ascending-distance order. Break ties by ascending index.

tests/adapters.ts
export function runBruteForce(query: number[], base: number[][], k: number): number[] {
  // return the k nearest base-row indices, nearest first
}
Tie-break on ascending index

When two base vectors are exactly equidistant from the query, return the one with the smaller index first. This documented tie-break is what makes the expected top-k a single deterministic answer — the same rule you will reuse inside every heap and search.

How it's graded

Adapter mode: your returned index list is compared exactly (atol 0) to the reference top-10. The fixture query is chosen so its top-10 is well-separated — no float ties to worry about here.