1/31
Part 1 of 6 · DCGLearn

Grade a ranked list

Search and ranking design rounds keep asking two things: how you grade a ranking, and why that grade can lie. The practice problem puts both in code, in four levels. Level 1 reads like this:

Each search returns a ranked list of results, and a judge grades each result from 0 to 3, where 3 is exactly right. Write two functions in NumPy.

  • dcg_at_k(rels, k) is the discounted cumulative gain of the top k results, where rels holds the grades in ranked order. Give each result a gain of 2^rel - 1, divide it by log2(position + 1) with positions counted from 1, and add them up.
  • ndcg_at_k(rels, k) divides that by the DCG of the best possible order of the same items. A list with nothing relevant scores 0.
CallReturns, rounded
dcg_at_k([3, 1, 2], 3)9.1309
dcg_at_k([3, 1, 2], 1)7.0
ndcg_at_k([3, 1, 2], 3)0.9721
ndcg_at_k([1, 3], 1)0.1429
ndcg_at_k([0, 0, 0], 3)0.0

Level 2 corrects click data for position bias, the extra clicks a result gets just for sitting on top. Level 3 measures how much attention each position gets, from a slice of traffic where the top results were shuffled. Level 4 caps the correction when the data is thin.

You don't write it yet. First you build each piece by hand: the gain, the discount, the best possible order and its edge cases, then the click model behind levels 2 to 4. At the end you solve the whole problem, level by level, in an interview simulation with a 50-minute timer.