Heap in Python

Last Updated :
Discuss
Comments

Question 1

What is a heap?

  • A

    A data structure that stores data in a binary tree structure

  • B

    A collection of data elements stored in an unordered manner

  • C

    A stack data structure with additional constraints

  • D

    A sorted collection of data elements

Question 2

Which of the following is the correct way to create a heap in Python?

  • A

    heap = []

  • B

    heap = [1, 2, 3, 4]

  • C

    from heapq import heapify; heapify([1, 2, 3, 4])

  • D

    heap = {1: 'a', 2: 'b', 3: 'c'}

Question 3

What is the time complexity of inserting an element into a heap?

  • A

    O(log n)

  • B

    O(n)

  • C

    O(1)

  • D

    O(n log n)

Question 4

Which of the following is the correct way to add an element to a heap?

  • A

    heap.append(5)

  • B

    heap.insert(5)

  • C

    from heapq import heappush; heappush(heap, 5)

  • D

    heap.add(5)

Question 5

What is the time complexity of removing the smallest element from a heap?

  • A

    O(log n)

  • B

    O(n)

  • C

    O(1)

  • D

    O(n log n)

Question 6

Which of the following is the correct way to remove the smallest element from a heap?

  • A

    heap.pop()

  • B

    heap.remove(0)

  • C

    from heapq import heappop; heappop(heap)

  • D

    heap.delete_min()

Question 7

What is the time complexity of finding the smallest element in a heap?

  • A

    O(log n)

  • B

    O(n)

  • C

    O(1)

  • D

    O(n log n)

Question 8

Which of the following is the correct way to find the smallest element in a heap?

  • A

    heap[0]

  • B

    min(heap)

  • C

    from heapq import heappush; heappush(heap, 5)

  • D

    heap.peek()

Question 9

How to get the smallest element from a heap without removing it?

  • A

    heap[0]

  • B

    peek_heap(heap)

  • C

    heap_peak(heap)

  • D

    heappop(heap)

Question 10

What is the advantage of using a heap over other data structures?

  • A

    Faster insertion and removal of elements

  • B

    Faster searching of elements

  • C

    Requires less memory

  • D

    None of the above

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion