ProgrammingModerateTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import heapq
events = [(3, 'end'), (1, 'start'), (2, 'mid')]
heapq.heapify(events)
out = []
while events:
    t, name = heapq.heappop(events)
    out.append(f'{name}@{t}')
print(' '.join(out))
  1. A. end@3 mid@2 start@1
  2. B. start@1 end@3 mid@2
  3. C. mid@2 start@1 end@3
  4. D. start@1 mid@2 end@3

Correct answer: D. start@1 mid@2 end@3

heappop returns the smallest tuple first, so events come out in order of time. The output is in ascending time order.

More Python Programming questions

Exams that ask this topic