Q. How do you remove the maximum element from a max-heap?
A.
Remove the root and re-heapify
B.
Remove the last element
C.
Swap the root with the last element
D.
Both A and C
Show solution
Solution
To remove the maximum element from a max-heap, you swap the root with the last element, remove the last element, and then re-heapify.
Correct Answer:
D
— Both A and C
Learn More →
Q. In a min-heap, which of the following is true?
A.
The root is the smallest element
B.
The root is the largest element
C.
All parent nodes are smaller than their children
D.
Both A and C
Show solution
Solution
In a min-heap, the root is the smallest element, and all parent nodes are smaller than their children.
Correct Answer:
D
— Both A and C
Learn More →
Q. In a min-heap, which of the following statements is true?
A.
The parent node is always greater than its children
B.
The parent node is always less than its children
C.
All nodes are in sorted order
D.
The smallest element is at the bottom
Show solution
Solution
In a min-heap, the parent node is always less than or equal to its children, ensuring that the smallest element is at the root.
Correct Answer:
B
— The parent node is always less than its children
Learn More →
Q. What is the maximum height of a binary heap with n elements?
A.
n
B.
log n
C.
n log n
D.
2n
Show solution
Solution
The maximum height of a binary heap is log n, as it is a complete binary tree.
Correct Answer:
B
— log n
Learn More →
Q. What is the primary difference between a binary heap and a binary search tree?
A.
Binary heaps are complete binary trees, while binary search trees are not
B.
Binary heaps allow duplicate elements, while binary search trees do not
C.
Binary heaps are used for priority queues, while binary search trees are used for searching
D.
All of the above
Show solution
Solution
The primary differences include that binary heaps are complete binary trees used for priority queues, while binary search trees are not necessarily complete and are used for searching.
Correct Answer:
D
— All of the above
Learn More →
Q. What is the primary use of a priority queue?
A.
Sorting elements
B.
Finding the shortest path
C.
Managing tasks based on priority
D.
Storing elements in a specific order
Show solution
Solution
A priority queue is used to manage tasks based on their priority, allowing for efficient retrieval of the highest (or lowest) priority task.
Correct Answer:
C
— Managing tasks based on priority
Learn More →
Q. What is the result of performing a 'decrease key' operation in a min-heap?
A.
The key is increased
B.
The key is decreased and the heap property is maintained
C.
The key is removed
D.
The heap is destroyed
Show solution
Solution
The 'decrease key' operation reduces the value of a key and may require re-heapifying to maintain the heap property.
Correct Answer:
B
— The key is decreased and the heap property is maintained
Learn More →
Q. What is the result of performing a heap sort on an array?
A.
An unsorted array
B.
A partially sorted array
C.
A sorted array
D.
A reverse sorted array
Show solution
Solution
Heap sort transforms the array into a max-heap and then repeatedly extracts the maximum element, resulting in a sorted array.
Correct Answer:
C
— A sorted array
Learn More →
Q. What is the space complexity of a binary heap storing n elements?
A.
O(1)
B.
O(n)
C.
O(log n)
D.
O(n log n)
Show solution
Solution
The space complexity of a binary heap storing n elements is O(n) because it needs to store all n elements.
Correct Answer:
B
— O(n)
Learn More →
Q. What is the space complexity of a binary heap?
A.
O(1)
B.
O(n)
C.
O(log n)
D.
O(n log n)
Show solution
Solution
The space complexity of a binary heap is O(n) because it stores n elements in an array.
Correct Answer:
B
— O(n)
Learn More →
Q. What is the time complexity of deleting the maximum element from a max-heap?
A.
O(1)
B.
O(log n)
C.
O(n)
D.
O(n log n)
Show solution
Solution
Deleting the maximum element from a max-heap involves removing the root and then re-heapifying, which takes O(log n) time.
Correct Answer:
B
— O(log n)
Learn More →
Q. What is the worst-case time complexity for building a heap from an array of n elements?
A.
O(n)
B.
O(n log n)
C.
O(log n)
D.
O(1)
Show solution
Solution
The worst-case time complexity for building a heap from an array of n elements is O(n) using the heapify process.
Correct Answer:
A
— O(n)
Learn More →
Q. Which algorithm is commonly used to convert an array into a heap?
A.
Heapify
B.
Merge Sort
C.
Quick Sort
D.
Insertion Sort
Show solution
Solution
The 'Heapify' algorithm is used to convert an array into a heap structure, ensuring the heap property is maintained.
Correct Answer:
A
— Heapify
Learn More →
Q. Which algorithm uses a priority queue to find the shortest path in a graph?
A.
Dijkstra's Algorithm
B.
Kruskal's Algorithm
C.
Prim's Algorithm
D.
Bellman-Ford Algorithm
Show solution
Solution
Dijkstra's Algorithm uses a priority queue to efficiently find the shortest path from a source vertex to all other vertices in a graph.
Correct Answer:
A
— Dijkstra's Algorithm
Learn More →
Q. Which of the following is a characteristic of a binary heap?
A.
Complete binary tree
B.
Balanced binary tree
C.
Binary search tree
D.
Unbalanced tree
Show solution
Solution
A binary heap is a complete binary tree, meaning all levels are fully filled except possibly for the last level.
Correct Answer:
A
— Complete binary tree
Learn More →
Q. Which of the following is NOT a valid implementation of a priority queue?
A.
Array
B.
Linked List
C.
Binary Search Tree
D.
Max-Heap
Show solution
Solution
While a binary search tree can be used to implement a priority queue, it is not a typical or efficient implementation compared to heaps.
Correct Answer:
C
— Binary Search Tree
Learn More →
Q. Which of the following is true about a max-heap?
A.
The parent node is always smaller than its children
B.
The parent node is always larger than its children
C.
It is a complete binary tree
D.
Both B and C
Show solution
Solution
In a max-heap, the parent node is always larger than its children, and it is also a complete binary tree.
Correct Answer:
D
— Both B and C
Learn More →
Q. Which of the following operations can be performed in O(1) time on a priority queue implemented with a max-heap?
A.
Insert
B.
Delete Max
C.
Get Max
D.
Decrease Key
Show solution
Solution
Getting the maximum element in a max-heap can be done in O(1) time since the maximum element is always at the root.
Correct Answer:
C
— Get Max
Learn More →
Q. Which of the following operations can be performed in O(log n) time in a binary heap?
A.
Insertion
B.
Deletion of the maximum element
C.
Heapify
D.
All of the above
Show solution
Solution
All of the listed operations (insertion, deletion of the maximum element, and heapify) can be performed in O(log n) time in a binary heap.
Correct Answer:
D
— All of the above
Learn More →
Showing 1 to 19 of 19 (1 Pages)