Mean reciprocal rank (MRR)
Background
Mean Reciprocal Rank evaluates systems that return a ranked list where you mostly care about the first correct answer — question answering, entity retrieval, "I'm feeling lucky" search. For each query the reciprocal rank is ; MRR averages it over all queries.
Problem statement
Implement mean_reciprocal_rank(rankings), where rankings[q] is a binary relevance list for query in ranked order (1 = relevant):
where is the 1-indexed position of the first relevant item in query 's list (the term is if no relevant item appears).
Input
rankings—list[list[int]]: each inner list holds 0/1 relevance flags in ranked order.
Output
Returns a float in .
Examples
Example 1
Input: rankings = [[0, 1, 0], [1, 0, 0], [0, 0, 1]]
Output: 0.6111
Explanation: the first relevant items sit at ranks 2, 1, 3, giving reciprocal ranks , , . Their mean is .
Constraints
- Ranks are 1-indexed (the top result is rank 1).
- A query with no relevant item contributes to the sum.
- MRR is the mean of the per-query reciprocal ranks; the result lies in .
Notes
- MRR rewards only the first hit — a perfect second result still caps a query's contribution at .
- Contrast with NDCG/MAP, which credit all relevant results; MRR is the right choice when there is essentially one correct answer per query.
This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Reference example: 0.6111
- •All first-position hits -> 1.0
- •A query with no relevant item contributes 0
- •Reciprocal rank uses the FIRST relevant position only