Compute NDCG for a Ranked List of Product Search Results

Last updated: November 29, 2026

Quick Overview

Given graded relevance labels for the products a search ranker returned for a query, compute NDCG@k by hand and in code, and explain why it is preferred over precision@k for Amazon search.

Amazon
Machine Learning
Data Scientist
Amazon
November 29, 2026
Data Scientist
Phone Screen
Machine Learning
Easy

0

0

0 solved


Given graded relevance labels for the products a search ranker returned for a query, compute NDCG@k by hand and in code, and explain why it is preferred over precision@k for Amazon search.

Search relevance teams at Amazon evaluate rankers with human-judged or behavior-derived graded labels, so a phone screen often checks that you can compute and interpret NDCG. The interviewer wants the formula, a clean implementation, and awareness of edge cases like queries with no relevant results.

What the Interviewer Expects
  • State the DCG formula with the gain and position discount
  • Normalize by the ideal ordering
  • Compute an example correctly by hand
  • Handle the zero-relevance query case
  • Explain when NDCG is and is not the right metric
Key Topics to Cover
NDCG
Graded relevance
Position discount
Ranking evaluation
How to Approach This
  1. Understand the bias-variance trade-off. High training accuracy but low test accuracy signals overfitting.
  2. Choose evaluation metrics carefully based on the problem. Accuracy alone is often insufficient.
  3. Feature engineering is often more impactful than model selection.
  4. Know when to use tree-based models (tabular data) vs neural networks (unstructured data).
  5. Handle class imbalance with SMOTE, class weights, or appropriate loss functions.
Possible Follow-up Questions
  • How would you derive graded labels from clicks, add-to-carts and purchases?
  • How do you average NDCG across queries with very different traffic?
  • Why might NDCG improve offline while revenue does not move?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Explore ML Interview Prep
Sample Answer
Clarify

Labels are graded, for example 3 = exact match, 2 = substitute, 1 = complement, 0 = irrelevant. Confirm k (the first page shows about 16 to 48 results, but mobile users mostly see the top 4 to 8) and ...

Approach

DCG@k=∑i=1k2ri−1log⁡2(i+1)\text{DCG@k}=\sum_{i=1}^{k}\frac{2^{r_i}-1}{\log_2(i+1)}. The ideal DCG sorts the same labels descending. NDCG@k=DCG@k/IDCG@k\text{NDCG@k}=\text{DCG@k}/\text{IDCG@k}, giving a score in [0,1][0,1] that is comparable acr...


Submit Your Answer
Markdown supported