MCQ on Heaps – Binary Heaps, Min-Heap, Max-Heap

Heaps are specialized tree-based data structures that have a variety of applications, especially in algorithms for finding the shortest path like Dijkstra’s. They can be visualized as binary trees but must satisfy the heap property. Dive into this MCQ set to test your knowledge about heaps!

1. Which of the following properties must be maintained in a Min-Heap?

a) Parent node is greater than child nodes
b) Parent node is less than child nodes
c) All nodes are in increasing order
d) All nodes are in decreasing order

Answer:

b) Parent node is less than child nodes

Explanation:

In a Min-Heap, for any given node I, the value of I is always less than or equal to its children.

2. Which operation has a time complexity of O(1) in a binary heap?

a) Insertion
b) Deletion
c) Finding minimum in Min-Heap
d) Balancing the heap

Answer:

c) Finding minimum in Min-Heap

Explanation:

In a Min-Heap, the root always contains the smallest element, and it can be retrieved in O(1) time.

3. What is the maximum number of children a node can have in a binary heap?

a) 1
b) 2
c) 3
d) 4

Answer:

b) 2

Explanation:

A binary heap is a binary tree; hence, each node can have a maximum of 2 children.

4. In a Max-Heap:

a) Every node is greater than its descendants
b) Every node is less than its descendants
c) The tree is always balanced
d) None of the above

Answer:

a) Every node is greater than its descendants

Explanation:

In a Max-Heap, for any given node I, the value of I is always greater than or equal to its children.

5. Which of the following is NOT a valid application of heap?

a) Finding shortest path in a graph
b) Priority queue implementation
c) Infix to postfix conversion
d) Finding kth largest element

Answer:

c) Infix to postfix conversion

Explanation:

Infix to postfix conversion is typically done using a stack. Heaps are not used for this purpose.

6. If the parent node is at index ‘I’ in a 0-indexed binary heap, where can you find its left child?

a) I
b) 2I
c) 2I + 1
d) I + 1

Answer:

c) 2I + 1

Explanation:

For a 0-indexed binary heap, the left child is found at 2I + 1.

7. Which of the following is true about a binary heap?

a) It’s always complete
b) It’s always perfect
c) It’s always balanced
d) It’s always full

Answer:

a) It’s always complete

Explanation:

A binary heap is always a complete binary tree, meaning every level of the tree is fully filled except possibly the last level, which is filled from left to right.

8. Which data structure is typically used to implement a heap?

a) Linked list
b) Array
c) Binary tree
d) Graph

Answer:

b) Array

Explanation:

Heaps are usually implemented using arrays because of their complete binary tree property, which allows easy access to parent and child nodes.

9. In a Min-Heap with elements: 1, 3, 5, 7, 9, 2, 4. Which one is the root?

a) 1
b) 3
c) 5
d) 7

Answer:

a) 1

Explanation:

In a Min-Heap, the root always contains the smallest element.

10. If the parent node is at index ‘I’ in a 1-indexed binary heap, where can you find its right child?

a) 2I
b) 2I + 1
c) 2I + 2
d) I + 1

Answer:

b) 2I + 1

Explanation:

For a 1-indexed binary heap, the right child is found at 2I + 1.

11. Heapify operation is used to:

a) Add an element to the heap
b) Remove an element from the heap
c) Balance a binary tree
d) Restore the heap property

Answer:

d) Restore the heap property

Explanation:

Heapify operation is used to reorganize the heap after insertion or removal of an element to ensure the heap property is maintained.

12. Which of these sorting algorithms uses a binary heap?

a) Bubble sort
b) Insertion sort
c) Merge sort
d) Heap sort

Answer:

d) Heap sort

Explanation:

Heap sort utilizes a binary heap (either Max-Heap or Min-Heap) to sort elements.

13. Deleting the maximum element from a Max-Heap has a time complexity of:

a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)

Answer:

b) O(log n)

Explanation:

Deleting the maximum element requires removing the root and then reorganizing the heap using heapify, which takes O(log n) time.

14. Which of the following algorithms can be optimized using a Min-Heap?

a) Binary Search
b) Breadth-first search
c) Dijkstra’s shortest path
d) Matrix multiplication

Answer:

c) Dijkstra’s shortest path

Explanation:

Dijkstra’s algorithm can be optimized using a Min-Heap to efficiently select the next node with the shortest distance.

15. In a Max-Heap, the maximum element can be found in:

a) Leaf node
b) Any random node
c) The last node
d) The root node

Answer:

d) The root node

Explanation:

In a Max-Heap, the maximum element is always found at the root.


Heaps are crucial when it comes to algorithms that require efficient maximum or minimum element look-up, insertion, and deletion. It’s essential to understand their properties and the difference between their types. Keep refining your understanding, and you’ll see how these structures play an essential role in various real-world applications!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top