NDCG at K for a Single Query

Quick Overview

Implement NDCG@k for one query in Python with an exponential gain, a log2 rank discount and stable tie-breaking. It tests ranking-metric basics, using the same cut-off for DCG and the ideal DCG, and careful handling of empty inputs, all-zero relevance and invalid arguments.

NDCG at K for a Single Query

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `ndcg_at_k(y_true_relevance, y_pred_scores, k)`, which returns NDCG@k as a Python `float` for one query. Rank items by descending `y_pred_scores`. Tied scores keep their original input order (stable sort). Use gain `2**rel - 1` and discount `1 / log2(rank + 1)` with 1-based ranks. - Cut-off is `m = min(k, n)`. IDCG sums the top-`m` gains of all items sorted in descending order. - Empty input, or IDCG of `0` (all-zero relevance), returns `0.0`. - Mismatched input shapes, or `k < 1`, raise `ValueError`. Example: ```python ndcg_at_k([1, 0], [0.2, 0.9], k=10) # order: rel 0, then rel 1 -> DCG = 1/log2(3) # IDCG = 1.0 # returns 0.6309... (1 / log2(3)) ``` ```hint Getting the order right Check whether the sort you plan to use is stable, and how to get a descending order from it without reversing the tied items. ``` ```hint One discount vector DCG and IDCG use the same ranks 1 to m. Build the discounts once and apply them to both sets of gains. ``` ### Constraints & Assumptions - Inputs are 1-D array-likes of equal length `n ≥ 0`. - Relevance values are non-negative. Scores are finite. - `k` is an integer and may exceed `n`. ### What a Strong Answer Covers - A descending ranking that keeps tied items in input order, and whether the default sort does that - DCG and IDCG truncated at the same cut-off, with IDCG built from the gains of all items - Input validation and the two cases that return `0.0`, including the order the checks run in - Time and space complexity - A hand check of the example, plus a case with tied scores ### Follow-up Questions - How would you handle ties without depending on input order? - How would you compute mean NDCG@k over many queries efficiently, and what do you do with queries whose IDCG is zero? - When does the exponential gain `2**rel - 1` pick a different winning model than the linear gain `rel`?

Overview: Implement NDCG@k for one query in Python with an exponential gain, a log2 rank discount and stable tie-breaking. It tests ranking-metric basics, using the same cut-off for DCG and the ideal DCG, and careful handling of empty inputs, all-zero relevance and invalid arguments.

General logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement ndcg_at_k(y_true_relevance, y_pred_scores, k), which returns NDCG@k as a Python float for one query. Rank items by descending y_pred_scores. Tied scores keep their original input order (stable sort). Use gain 2**rel - 1 and discount 1 / log2(rank + 1) with 1-based ranks.

  • Cut-off is m = min(k, n) . IDCG sums the top- m gains of all items sorted in descending order.
  • Empty input, or IDCG of 0 (all-zero relevance), returns 0.0 .
  • Mismatched input shapes, or k < 1 , raise ValueError .

Example:

ndcg_at_k([1, 0], [0.2, 0.9], k=10)
# order: rel 0, then rel 1  -> DCG = 1/log2(3)
# IDCG = 1.0
# returns 0.6309...  (1 / log2(3))

Constraints & Assumptions

  • Inputs are 1-D array-likes of equal length n ≥ 0 .
  • Relevance values are non-negative. Scores are finite.
  • k is an integer and may exceed n .

What a Strong Answer Covers Guidance

  • A descending ranking that keeps tied items in input order, and whether the default sort does that
  • DCG and IDCG truncated at the same cut-off, with IDCG built from the gains of all items
  • Input validation and the two cases that return 0.0 , including the order the checks run in
  • Time and space complexity
  • A hand check of the example, plus a case with tied scores

Follow-up Questions Guidance

  • How would you handle ties without depending on input order?
  • How would you compute mean NDCG@k over many queries efficiently, and what do you do with queries whose IDCG is zero?
  • When does the exponential gain 2**rel - 1 pick a different winning model than the linear gain rel ?