mediumHeap / Priority QueueAI-applied~22 min
Kth Confidence Score
A classifier emits a confidence score per candidate label. A re-ranking step needs the k-th highest confidence as a cutoff threshold. The scores are just numbers — the task is the classic k-th largest selection.
Problem
Given an array of scores and an integer k, return the k-th largest value (in sorted-descending order, with duplicates counted by position).
Input
An array scores of length n (1 ≤ k ≤ n ≤ 10^5), values in [-10^4, 10^4].
Output
A single integer — the k-th largest score.
Constraints
- 1 ≤ k ≤ n ≤ 100,000
- Duplicates count toward the ordinal position
- Aim better than a full sort when k is small
Examples
Example 1
Input
scores = [3, 2, 1, 5, 6, 4], k = 2
Output
5
Sorted descending: 6, 5, 4, 3, 2, 1 — the 2nd is 5.
Example 2
Input
scores = [3, 2, 3, 1, 2, 4, 5, 5, 6], k = 4
Output
4
Sorted descending: 6,5,5,4,3,3,2,2,1 — the 4th is 4.