Ranking Metrics interview question

Machine LearningRanking Metricshard

For the query "waterproof hiking boots", assessors graded four products as relevant: Boot-X (3), Boot-Y (3), Boot-Z (2) and Sock-K (1); every other product is grade 0. The search returned the top 5 below, and Boot-Y was not retrieved. With gain 2^rel − 1 and discount log₂(rank + 1), what is nDCG@5?

RankReturned productGrade
1Boot-X3
2Rain cover0
3Boot-Z2
4Sock-K1
5Insole0
  1. A≈ 0.67: IDCG@5 is the DCG of the best possible top 5 from all judged products, so the unretrieved grade-3 Boot-Y takes rank 2 of the ideal list.
  2. B≈ 0.95: IDCG@5 is the DCG of the five returned products re-sorted by grade, because nDCG only measures how well the retrieved items are ordered.
  3. C≈ 0.50: IDCG is the total gain of all judged products, 7 + 7 + 3 + 1 = 18, because a perfect system would retrieve every one of them in the top 5.
  4. D≈ 0.43: IDCG@5 assumes a grade-3 product in each of the five slots, because nDCG compares the list with the best page the grading scale allows.
Show the answer and explanation

Correct answer: A, ≈ 0.67: IDCG@5 is the DCG of the best possible top 5 from all judged products, so the unretrieved grade-3 Boot-Y takes rank 2 of the ideal list.

Explanation Ranking Metrics

nDCG@k = DCG@k / IDCG@k. DCG sums each result's gain divided by a position discount. IDCG is the DCG of the ideal ranking: the best possible top k built from all judged documents for the query, not only the ones the system returned.

DCG of the returned list (gain 2^rel − 1, discount log₂(rank + 1)):

  • rank 1, Boot-X: 7 / 1 = 7
  • rank 3, Boot-Z: 3 / 2 = 1.5
  • rank 4, Sock-K: 1 / log₂5 ≈ 0.431
  • DCG@5 ≈ 8.93

Ideal top 5 = grades 3, 3, 2, 1, 0: 7 + 7/log₂3 + 3/2 + 1/log₂5 ≈ 7 + 4.417 + 1.5 + 0.431 ≈ 13.35. So nDCG@5 ≈ 8.93 / 13.35 ≈ 0.67.

Why the other values are wrong:

  • 0.95 re-sorts only the returned items (IDCG ≈ 9.39). That hides the missed grade-3 boot, which is exactly the failure the metric should penalise.
  • 0.50 drops the discount from the ideal list. IDCG must use the same discounts as DCG, otherwise even a perfect ranking scores below 1 (13.35 / 18 ≈ 0.74).
  • 0.43 normalises by an imaginary page of five grade-3 products. Only two exist for this query, so a perfect ranking would again score below 1.

Interview tip: when you review an nDCG pipeline, ask where the IDCG comes from. Code that builds it from the returned list (or a truncated candidate set) silently inflates scores and hides recall failures.

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Machine Learning questions

  1. A gradient-boosted tree model was trained on months 1–36 of demand, using month_index as a feature. Its forecast for months 37–48 is the flat line in the…
  2. A telecom churn model (3% churners) is evaluated with the code below and reports a mean CV recall of 0.99. On customers that were set aside before any…
  3. An insurer has 3,000 claims confirmed as fraud and 120,000 claims nobody has investigated. A calibrated classifier trained to separate confirmed (s = 1) from…
  4. A helpdesk routes tickets with a KNN classifier using Euclidean distance on TF-IDF vectors built with norm=None (no length normalisation). Tickets range from…
  5. For a declined credit-card application, an Anchors explainer returns the rule IF utilization > 0.80 AND missed_payments_12m ≥ 2 THEN decline, with precision…