Python Heapq

Last Updated :
Discuss
Comments

Question 1

What is the primary characteristic of a min-heap data structure?

  • A

    The smallest element is at the leaf nodes.

  • B

    Each parent node is larger than its children.

  • C

    The smallest element is always at the root.

  • D

    Elements are stored in a random order.

Question 2

Which function in the heapq module is used to add an element while maintaining the heap property?

  • A

    heapq.pop()

  • B

    heapq.insert()

  • C

    heapq.heappush()

  • D

    heapq.add()

Question 3

What operation does the heapq.heapreplace() function perform?

  • A

    It only adds an element to the heap.

  • B

    It pops the largest element from the heap.

  • C

    It pops the smallest element and adds a new element.

  • D

    It merges two heaps into one.

Question 4

Which of the following functions allows retrieval of the n largest elements from a heap?

  • A

    heapq.nlargest()

  • B

    heapq.getlargest()

  • C

    heapq.maxelements(n)

  • D

    heapq.retrievelargest()

Question 5

What is a disadvantage of using a heap queue?

  • A

    It supports random access to elements.

  • B

    It allows efficient sorting of all elements.

  • C

    It is not thread-safe.

  • D

    It requires more memory than linked lists.

Question 6

In Python's heapq module, which function is used to merge multiple sorted iterables into a single sorted heap?

  • A

    heapq.combine()

  • B

    heapq.merge()

  • C

    heapq.concat()

  • D

    heapq.join()

Question 7

When using the heappop() function, what is the result?

  • A

    It adds a new element to the heap.

  • B

    It returns the largest element in the heap.

  • C

    It removes and returns the smallest element in the heap.

  • D

    It checks the size of the heap.

Question 8

Which of the following statements about heaps is true?

  • A

    Heaps can be implemented using binary trees only.

  • B

    Heaps support random access to elements efficiently.

  • C

    Heaps do not allow duplicate elements.

  • D

    Heaps can be implemented using lists in Python.

Question 9

What is the time complexity of the heappush() operation in a heap?

  • A

    O(1)

  • B

    O(n)

  • C

    O(log n)

  • D

    O(n log n)

Question 10

Which heap operation is more efficient when replacing the smallest element with a new value?

  • A

    Using heappop() followed by heappush()

  • B

    Using heapq.replace()

  • C

    Using heappushpop()

  • D

    Using heapq.merge()

There are 11 questions to complete.

Take a part in the ongoing discussion