🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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?

  1. A.sorted(scores)[-5:]
  2. B.heapq.nlargest(5, scores)
  3. C.bisect.insort for every score
  4. 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 →