LearnFrenzy
Sign in Take a free test

Python Coding Interview Questions · Find the Bug · Question 9

Error: heap entries that cannot be compared Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

The program below stops with an error. Which statement correctly describes the problem?

main.py
import heapq
tasks = []
heapq.heappush(tasks, (2, {"name": "deploy"}))
heapq.heappush(tasks, (2, {"name": "review"}))
print(heapq.heappop(tasks))
  • A It prints (2, {'name': 'deploy'})
  • B KeyError
  • C IndexError
  • D TypeError: equal priorities make heapq compare the dicts, which do not support <; add a counter as a tie-breaker: (priority, count, task) Correct
Correct Answer: Option D

Tuples compare item by item; a unique second item (an increasing counter) stops the comparison before it reaches the dict.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links