Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

43. Mean Reciprocal Rank

easy
GeneralGeneral
senior

Mean Reciprocal Rank (MRR) is a common evaluation metric for NLP retrieval and ranking tasks where each query has exactly one correct answer. You’ll compute MRR from ranked prediction lists by finding the rank position of the first relevant item per query.

Requirements

Implement the function

python

Rules:

  • For each query q, find the 1-based index of targets[q] inside rankings[q], then compute its reciprocal.
  • If the target is not present in the ranking list, its reciprocal rank is 0.0.
  • Return the average reciprocal rank across all queries.
  • Use NumPy for vectorized operations where helpful.
  • Don’t use any prebuilt ranking-metric functions (e.g., from sklearn or torchmetrics).

Example

python

Output:

python
Input Signature
ArgumentType
targetsnp.ndarray
rankingslist[np.ndarray]
Output Signature
Return NameType
valuefloat

Constraints

  • Input rankings is a list of NumPy arrays; targets is a NumPy array

  • No sklearn/torchmetrics ranking functions

  • Missing target contributes reciprocal rank 0.0

Hint 1

MRR is the average over queries of 1 / (1-based rank of the target); if the target isn’t in the list, contribute 0.

Hint 2

For each (rank_list, target), find the target’s index. In Python you can use list.index with a try/except, or with NumPy use np.where(rank_list == target).

Hint 3

Implementation pattern: loop with zip(rankings, targets), compute recip = 0.0 if no match else 1.0/(idx+1), append, then return float(np.mean(recip_ranks)).

Roles
ML Engineer
AI Engineer
Data Scientist
Quantitative Analyst
Companies
GeneralGeneral
Levels
senior
entry
Tags
mean-reciprocal-rank
ranking-metrics
numpy
information-retrieval
49 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit