Quiz League Final Cutoff
A pub quiz league sends its best k teams to a grand final. Teams finish their seasons on different nights, and each season total is posted once final. Wrong answers cost a point, so a total can be negative.
The league website shows the cutoff, which is the total a team currently needs to be in the top k. The cutoff must update as soon as each new total arrives.
Your task
Write a class FinalCutoff:
constructor(k, totals)receivesk, the number of final places, andtotals, the list of totals already posted, which may be empty.add(total)records one more posted total and then returns thek-th highest total posted so far. If fewer thanktotals have been posted, it returnsnull(Nonein Python).
Equal totals take separate places: if the top totals are 50, 50 and 40, the 2nd highest is 50 and the 3rd is 40.
class FinalCutoff
Examples
new FinalCutoff(3, [42, 17, 30]) .add(25) .add(50) .add(10) .add(42)
[null, 25, 30, 30, 42]
The top 3 starts as 42, 30 and 17. Adding 25 pushes out 17, so the cutoff is 25. Adding 50 pushes out 25, so the cutoff is 30. Adding 10 is too low to enter the top 3, so the cutoff stays 30. Adding 42 pushes out 30, so the top 3 is 50, 42 and 42, and the cutoff is 42.
new FinalCutoff(2, []) .add(-5) .add(8) .add(3)
[null, null, -5, 3]
With one total there is no 2nd place yet, so the result is null. After 8 is added, the top 2 is 8 and -5, so the cutoff is -5. Then 3 pushes -5 out, so the cutoff becomes 3.
Constraints
- 1 ≤ k ≤ 10,000
- 0 ≤ totals.length ≤ 10,000
- -1,000 ≤ each total ≤ 1,000
- At most 10,000 calls to add.
Start with brute force
The brute force keeps every total in a list. On each add it appends the total, sorts the list from high to low, and reads position k - 1.
Each call costs O(n log n) for n totals so far. With 10,000 starting totals and 10,000 calls, each sort handles between 10,000 and 20,000 totals, which is about 2 billion steps for the season. Inserting into a sorted list is better, but shifting the elements still costs O(n) per call.
Hints
0 of 4 shown- Hint 1
In the first example, once the top 3 is 42, 30, 25, a total of 10 arrives. Can a total below the current cutoff ever matter again?
- Hint 2
A total that falls out of the top
knever comes back, because new totals only push the cutoff up. So you only need to keepktotals. - Hint 3
The answer is the smallest of the
ktotals you keep. Each new total is compared with it and may replace it. Which data structure gives you the smallest item and lets you replace it in O(log k)? - Hint 4
Keep a min-heap of at most
ktotals. To add a total, push it, and if the heap now holds more thankitems, pop the smallest. If the heap holds exactlykitems, its top is the answer, and otherwise return null. Load the starting totals the same way.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
You only need the current top k totals, and among those the weakest one, because it is the cutoff and the first to be pushed out. A min-heap of size k keeps that total on top.
Trace the first example with k = 3:
- The constructor pushes 42, 17 and 30. The heap holds 17, 30 and 42, with 17 on top.
- add(25) pushes 25, so the heap has 4 items, and then pops the smallest, 17. The heap holds 25, 30 and 42, and the call returns 25.
- add(50) pushes 50 and pops 25. The heap holds 30, 42 and 50, and the call returns 30.
- add(10) pushes 10 and immediately pops it again, so the heap is unchanged and the call returns 30.
- add(42) pushes 42 and pops 30. The heap holds 42, 42 and 50, and the call returns 42.
To track the highest totals you use a min-heap, because a max-heap would put the highest total on top, and that is the one total you never need to look at.
class FinalCutoff {
constructor(k, totals) {
this.k = k;
this.heap = []; // min-heap of the k highest totals
for (const t of totals) this.add(t);
}
add(total) {
const h = this.heap;
h.push(total); // sift up
for (let i = h.length - 1; i > 0; ) {
const p = (i - 1) >> 1;
if (h[p] <= h[i]) break;
[h[p], h[i]] = [h[i], h[p]];
i = p;
}
if (h.length > this.k) { // drop the smallest: move last to top, sift down
h[0] = h.pop();
for (let i = 0; ; ) {
const l = 2 * i + 1, r = l + 1;
let m = i;
if (l < h.length && h[l] < h[m]) m = l;
if (r < h.length && h[r] < h[m]) m = r;
if (m === i) break;
[h[m], h[i]] = [h[i], h[m]];
i = m;
}
}
return h.length === this.k ? h[0] : null;
}
}import heapq
class FinalCutoff:
def __init__(self, k, totals):
self.k = k
self.heap = [] # min-heap of the k highest totals
for t in totals:
self.add(t)
def add(self, total):
heapq.heappush(self.heap, total)
if len(self.heap) > self.k:
heapq.heappop(self.heap) # drop the smallest
return self.heap[0] if len(self.heap) == self.k else NoneRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.