All problems
EasyMetricsv2026-06-27

Metrics ยท Easy

Recall@K

Measure how many relevant items appear in the top k results.

Task

Implement recall_at_k(labels: list[int], scores: list[float], k: int) -> float. Labels are binary, where 1 means relevant. Rank examples by descending score and return the fraction of all relevant examples recovered in the top k.

Requirements

  • Return 0.0 when k <= 0.
  • Return 0.0 when the input contains no relevant examples.
  • When k exceeds the dataset size, use every example.
  • Preserve original input order when scores tie.

Example

labels = [0, 1, 1, 0]
scores = [0.2, 0.9, 0.4, 0.7]

recall_at_k(labels, scores, 2)
# 0.5
solution.pySign in to save
Public tests run locally in your browser.
Run your code to see public test results.