← Quiz: Sorting, heapq and bisect: all 10 questions
Quiz: Sorting, heapq and bisect · Question 10 of 10
You need the 5 highest scores from one million numbers. What is the best choice?
You need the 5 highest scores from one million numbers. What is the best choice?
- A.sorted(scores)[-5:]
- B.heapq.nlargest(5, scores)
- C.bisect.insort for every score
- D.A while loop with max() and remove()
Answer & explanation
Correct answer: heapq.nlargest(5, scores)
nlargest keeps only a small heap of size k, which costs O(n log k) instead of sorting everything.
Can you get the rest right without peeking?
Take the timed Quiz: Sorting, heapq and bisect quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →