← Quiz: Heaps and Heap Sort Internals: all 15 questions
Quiz: Heaps and Heap Sort Internals · Question 13 of 15
What does this print? (code)
What does this print?
import heapq
nums = [7, 2, 9, 4, 6, 1]
heapq.heapify(nums)
print(nums[0], heapq.nlargest(2, nums), heapq.nsmallest(2, nums))
- A.7 [9, 7] [1, 2]
- B.1 [7, 9] [1, 2]
- C.1 [9, 7] [2, 1]
- D.1 [9, 7] [1, 2]
Answer & explanation
Correct answer: 1 [9, 7] [1, 2]
heapify turns the list into a min-heap in O(n). nlargest returns largest first; nsmallest returns smallest first.
Can you get the rest right without peeking?
Take the timed Quiz: Heaps and Heap Sort Internals quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →