Popularity Baseline: Most Interactions First

Recommender SystemsEasy+10 XP+10

Popularity Baseline: Most Interactions First

Before any personalisation, every recommender needs a popularity baseline: rank items by how many people interacted with them and show everybody the same list. It's also the fallback for a brand-new user you know nothing about.

Task: write popularity_rank(interactions) returning the item ids ordered most popular first.

  • interactions is a list of [user, item] pairs, one per interaction.
  • Count how many interactions each item received, and rank by that count, highest first.
  • If two items have the same count, the smaller id comes first.
  • Only items that appear at least once are in the output.

The tie-break matters more here than anywhere, because popularity counts tie constantly in the long tail — thousands of items with exactly one interaction each. Without a rule, their order is whatever the dictionary happens to produce.

Worth knowing what this baseline does and doesn't do. It's genuinely hard to beat on raw accuracy metrics, because popular things are popular for a reason, and it cold-starts perfectly. But it recommends the same thing to everyone, so it has zero personalisation and it actively reinforces itself: showing popular items makes them more popular, which pushes them further up the list. That feedback loop is why catalogue coverage and novelty get measured alongside accuracy — accuracy alone would tell you this baseline is excellent.

Examples

interactions = [['u1', 'a'], ['u2', 'a'], ['u1', 'b']]
returns ['a', 'b']
interactions = [['u', 'z'], ['u', 'a']]
returns ['a', 'z']

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