Top-k selection
# grade this stage (from your course directory)
npx theinference test --stage top-k --watchA ranking is the top-k highest-scoring docs, best first. Given the scores from scoreAll, topK returns the indices of the k largest, sorted by descending score. The subtlety that makes this gradeable: ties. When two docs tie, we break by ascending index — a fixed rule so there's exactly one correct answer.
What to build
Implement topK
Implement topK(scores, k) in src/index.ts: return a number[] of length k — the indices of the k highest scores, descending; ties broken by ascending index.
export function topK(scores: Float32Array, k: number): number[] {
// indices of the k largest, score desc, ties -> index asc
// e.g. build idx = [0..n), sort by (scores[y]-scores[x]) || (x-y), slice(0,k)
}
adapter with atol=0, rtol=0 (integer-exact): your returned index list must equal the reference exactly for k=10. The fixture score vector is chosen so its top-10 is well-separated (the rank-10 vs rank-11 gap is > 1e-4), so no float wobble can change the answer — only a wrong tie-break or wrong order can fail it.
The clean way: build [index, score] pairs and sort with (x, y) => scores[y] - scores[x] || x - y. The || (x − y) clause is the tie-break — drop it and your output is nondeterministic (the JS sort is not guaranteed stable across engines for the equal-score case) and fails grading even when the math is right.