Precision@K and Recall@K for Recommendations

Quick Overview

Implement precision@k and recall@k for ranked recommendation lists, averaged over users who have at least one relevant item. It tests careful hit counting with duplicates, fixed denominators, input validation and the averaging choices behind offline recommender metrics.

Precision@K and Recall@K for Recommendations

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Given per-user ranked lists `recommended`, per-user collections `relevant`, and an integer `k`, implement `precision_at_k` and `recall_at_k`. Each returns the mean per-user score as a Python `float`, averaged only over users with at least one relevant item. - `hits` = number of distinct relevant items in the first `k` recommendations (repeated items count once). - Precision = `hits / k` (always divide by `k`, even if the list is shorter). - Recall = `hits / |set(relevant)|` (duplicate relevant entries count once). - If no user has relevant items, return `0.0`. Example: ```python recommended = [[1, 2, 3, 4, 5], [20, 21], [6, 7, 8, 9, 10]] relevant = [{1, 3, 11}, set(), {7}] # user 2 skipped precision_at_k(recommended, relevant, 3) # (2/3 + 1/3) / 2 = 0.5 recall_at_k(recommended, relevant, 3) # (2/3 + 1/1) / 2 = 0.8333... precision_at_k([["a", "b"]], [{"a", "c"}], 5) # 1/5 = 0.2 ``` ```hint Watch the duplicates Repeats can show up in the recommendation list and in the relevant collection. Decide how each one affects the hit count and the recall denominator before you divide. ``` ### Constraints & Assumptions - Inner collections may be Python lists, sets or NumPy arrays of hashable items. The order of each `recommended` entry is its ranking, so the top `k` are its first `k` items. - `k` must be a positive integer, otherwise raise `ValueError`. - If `len(recommended) != len(relevant)`, raise `ValueError`. ### What a Strong Answer Covers - Counting distinct relevant hits in the first `k` recommendations, with repeated recommendations and repeated relevant entries each counted once. - The fixed denominators: `k` for precision even when the list is shorter, the number of distinct relevant items for recall. - Which users enter the average, why users with no relevant items are skipped, and the `0.0` result when none qualify. - Validation of `k` and of mismatched lengths, and a Python `float` return value. - Time and space complexity per user and overall. ### Follow-up Questions - Why might skipping users with no relevant items bias comparisons between models? - How does this relate to hit-rate@k, and when would you prefer it?

Overview: Implement precision@k and recall@k for ranked recommendation lists, averaged over users who have at least one relevant item. It tests careful hit counting with duplicates, fixed denominators, input validation and the averaging choices behind offline recommender metrics.

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

Given per-user ranked lists recommended, per-user collections relevant, and an integer k, implement precision_at_k and recall_at_k. Each returns the mean per-user score as a Python float, averaged only over users with at least one relevant item.

  • hits = number of distinct relevant items in the first k recommendations (repeated items count once).
  • Precision = hits / k (always divide by k , even if the list is shorter).
  • Recall = hits / |set(relevant)| (duplicate relevant entries count once).
  • If no user has relevant items, return 0.0 .

Example:

recommended = [[1, 2, 3, 4, 5], [20, 21], [6, 7, 8, 9, 10]]
relevant    = [{1, 3, 11}, set(), {7}]   # user 2 skipped
precision_at_k(recommended, relevant, 3)  # (2/3 + 1/3) / 2 = 0.5
recall_at_k(recommended, relevant, 3)     # (2/3 + 1/1) / 2 = 0.8333...

precision_at_k([["a", "b"]], [{"a", "c"}], 5)  # 1/5 = 0.2

Constraints & Assumptions

  • Inner collections may be Python lists, sets or NumPy arrays of hashable items. The order of each recommended entry is its ranking, so the top k are its first k items.
  • k must be a positive integer, otherwise raise ValueError .
  • If len(recommended) != len(relevant) , raise ValueError .

What a Strong Answer Covers Guidance

  • Counting distinct relevant hits in the first k recommendations, with repeated recommendations and repeated relevant entries each counted once.
  • The fixed denominators: k for precision even when the list is shorter, the number of distinct relevant items for recall.
  • Which users enter the average, why users with no relevant items are skipped, and the 0.0 result when none qualify.
  • Validation of k and of mismatched lengths, and a Python float return value.
  • Time and space complexity per user and overall.

Follow-up Questions Guidance

  • Why might skipping users with no relevant items bias comparisons between models?
  • How does this relate to hit-rate@k, and when would you prefer it?