Hit Rate: Did the Hidden Item Make the Top K?

Recommender SystemsEasy+10 XP+10

Hit Rate: Did the Hidden Item Make the Top K?

The bluntest possible recommender metric, and often the most honest one: for each user, you hid one item they actually went on to interact with. Did your top k contain it?

HitRate@k=users whose held-out item appeared in their top ktotal usersHitRate@k = \frac{\text{users whose held-out item appeared in their top } k}{\text{total users}}HitRate@k=total usersusers whose held-out item appeared in their top k​

Task: write hit_rate_at_k(recommended, holdout, k) returning that fraction, rounded to 4 decimal places.

  • recommended is a list of ranked recommendation lists, one per user, best first.
  • holdout[i] is the single item hidden from user i.
  • A user counts as a hit if their held-out item appears anywhere in their first k recommendations. Where in the top k it landed makes no difference.
  • Recommendation lists may be longer than k — truncate before checking.
  • Every user gets exactly one hit or one miss, so the result is between 0 and 1.

The truncation is the whole test. An item sitting at position 3 is a hit at k = 3 and a miss at k = 2, which is why quoting a hit rate without its k is meaningless.

What this metric deliberately ignores is rank within the top k — first place and last place score the same. That's the right simplification when the user sees all k at once, like a grid of thumbnails, and the wrong one for a long scrolling feed, where NDCG's position discount is what you want instead.

Examples

recommended = [['a', 'b', 'c'], ['x', 'y', 'z']]
holdout = ['b', 'q']
k = 2
returns 0.5
recommended = [['a', 'b', 'c']]
holdout = ['c']
k = 2
returns 0.0

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run