The gold-standard retrieval metric used by search engines and RAG evaluations. Rewards highly relevant results appearing at the top.
`DCG@K = Σ rel[i] / log₂(i+2)` (i is 0-indexed)
NDCG@K = DCG@K / IDCG@KWhere IDCG is the DCG of the ideal (perfectly sorted) ranking.
# relevance scores: [3, 2, 3, 0, 1, 2] ndcg([3,2,3,0,1,2], k=4) # DCG = 3/log2(2) + 2/log2(3) + 3/log2(4) + 0/log2(5) # IDCG = ideal DCG for sorted [3,3,2,2,1,0] → 0.78514
Round to **5 decimal places**.
Similar Problems
Test Cases (2 visible · 2 hidden)
Case 1: Standard NDCG
Input: ndcg([3,2,3,0,1,2],4)
Expected: 0.78514
Case 2: Perfect ranking
Input: ndcg([3,3,2,2],4)
Expected: 1.0
⌘↵ Run · ⌘⇧↵ Submit