Skip to content
Jobless Busters
Runs testsBasic15–25 min

Does the search find the right material?

Task: given the correct document IDs and a list of search results, compute the share that was found and the position of the first hit. Decide what to do with duplicates and a question with no correct answer.

Tests: understanding the metrics, not just the formula.

recall@k says what share of the needed documents is in the first k results. Reciprocal rank for one question is the inverse of the first hit's position; MRR is the average of such scores over many questions.

Contract: retrievalMetrics(relevant, ranked, k) returns {recallAtK, reciprocalRankAtK}. Duplicates are removed before taking the top k; with no relevant documents recallAtK is null, and an invalid k throws RangeError.

Follow-up: how would you evaluate correct refusals separately?

  • Compute recall@k
  • Compute reciprocal rank
  • Handle duplicates and the absence of relevant documents
  • retrievalMetrics(['A','B'], ['X','A','A','B'], 3)→ { recallAtK: 1, reciprocalRankAtK: 0.5 }
  • retrievalMetrics([], ['X'], 1)→ { recallAtK: null, reciprocalRankAtK: 0 }
The 1 tests

Tests ported from the source material's reference.test.ts (node:test). In the browser they run with the same set of assertions.

eval: dedupe, recall, MRR and a question with no evidence

JavaScript
assert.deepEqual(retrievalMetrics(['A', 'B'], ['X', 'A', 'A', 'B'], 3), { recallAtK: 1, reciprocalRankAtK: 0.5 });
assert.deepEqual(retrievalMetrics(['A', 'B'], ['X', 'A'], 2), { recallAtK: 0.5, reciprocalRankAtK: 0.5 });
assert.deepEqual(retrievalMetrics([], ['X'], 1), { recallAtK: null, reciprocalRankAtK: 0 });
assert.throws(() => retrievalMetrics(['A'], [], 0), RangeError);

Source: AI & Agents — practice tasks — An exercise written for the study programme, not a confirmed interview task.

JavaScript · runs in a Web Worker in your browser

Tab indents · Esc, then Tab, leaves the editor · ⌘/Ctrl + Enter runs the tests · Your code is saved in this browser.

Run the tests to check your solution. 1 predefined tests.

Stuck? Hints go from gentle to specific.

Give it a real try first — the solution is most useful after you've struggled a bit.