Dataset-Level MRR and Hit Rate@K

Quick Overview

Implement a function that computes Mean Reciprocal Rank and Hit Rate@K over a set of retrieval queries. It tests the difference between full-list and top-k windows, how queries with no relevant results are counted, input validation and careful 1-based ranking.

Dataset-Level MRR and Hit Rate@K

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `evaluate_retrieval(results, k)`, where `results` is an iterable of `(retrieved_ids, ground_truth_ids)` pairs. Return a dict `{"mrr": float, "hit_rate": float}` averaged over all queries. - MRR: reciprocal of the 1-based rank of the first relevant item in the full retrieved list (not truncated at `k`). - Hit Rate@K: fraction of queries with at least one relevant item in the top `k` (position `k` inclusive). - Queries with no relevant item retrieved, or empty ground truth, contribute 0 but still count in the denominator. For example: ```python results = [(["a", "b", "c"], ["c"]), (["x", "y"], ["x"]), (["p", "q", "r"], ["z"])] evaluate_retrieval(results, 2) # {"mrr": 0.4444..., "hit_rate": 0.3333...} # mrr = (1/3 + 1 + 0) / 3, hit_rate = 1 hit / 3 queries ``` ```hint Two windows, one denominator Reciprocal rank and the hit check look at different slices of the retrieved list. Before you average, decide which queries belong in the count you divide by. ``` ### Constraints & Assumptions - Ground truth may be any iterable (list, set, tuple). Retrieved lists may be empty or shorter than `k`. - Empty `results` returns `0.0` for both metrics. - A `k` that is not a positive integer raises `ValueError`. - Values must be Python `float`s. ### What a Strong Answer Covers - Reciprocal rank from the 1-based position of the first relevant item, searched over the whole retrieved list. - A hit test limited to the top `k`, with position `k` included and lists shorter than `k` handled. - Queries that score 0 (no relevant result, empty ground truth) kept in the denominator, and `0.0` for empty `results`. - Validation of `k`, Python `float` outputs, and the cost of the membership checks. ### Follow-up Questions - How would you add NDCG@K with graded relevance? - Why might MRR mislead when queries have many relevant documents?

Overview: Implement a function that computes Mean Reciprocal Rank and Hit Rate@K over a set of retrieval queries. It tests the difference between full-list and top-k windows, how queries with no relevant results are counted, input validation and careful 1-based ranking.

General logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement evaluate_retrieval(results, k), where results is an iterable of (retrieved_ids, ground_truth_ids) pairs. Return a dict {"mrr": float, "hit_rate": float} averaged over all queries.

  • MRR: reciprocal of the 1-based rank of the first relevant item in the full retrieved list (not truncated at k ).
  • Hit Rate@K: fraction of queries with at least one relevant item in the top k (position k inclusive).
  • Queries with no relevant item retrieved, or empty ground truth, contribute 0 but still count in the denominator.

For example:

results = [(["a", "b", "c"], ["c"]), (["x", "y"], ["x"]), (["p", "q", "r"], ["z"])]
evaluate_retrieval(results, 2)
# {"mrr": 0.4444..., "hit_rate": 0.3333...}
# mrr = (1/3 + 1 + 0) / 3, hit_rate = 1 hit / 3 queries

Constraints & Assumptions

  • Ground truth may be any iterable (list, set, tuple). Retrieved lists may be empty or shorter than k .
  • Empty results returns 0.0 for both metrics.
  • A k that is not a positive integer raises ValueError .
  • Values must be Python float s.

What a Strong Answer Covers Guidance

  • Reciprocal rank from the 1-based position of the first relevant item, searched over the whole retrieved list.
  • A hit test limited to the top k , with position k included and lists shorter than k handled.
  • Queries that score 0 (no relevant result, empty ground truth) kept in the denominator, and 0.0 for empty results .
  • Validation of k , Python float outputs, and the cost of the membership checks.

Follow-up Questions Guidance

  • How would you add NDCG@K with graded relevance?
  • Why might MRR mislead when queries have many relevant documents?