Q. If you have a priority queue implemented as a binary heap, what is the time complexity of finding the k-th smallest element?
-
A.
O(k log n)
-
B.
O(n)
-
C.
O(k)
-
D.
O(log n)
Solution
Finding the k-th smallest element using a priority queue requires extracting the minimum k times, resulting in O(k log n) time complexity.
Correct Answer:
A
— O(k log n)
Learn More →
Q. In a max-heap, if the root node has a value of 20, what can be the maximum value of its children?
Solution
In a max-heap, the value of a parent node must be greater than or equal to its children, so the maximum value of the children can be 20.
Correct Answer:
C
— 20
Learn More →
Q. In a max-heap, what is the relationship between the height of the heap and the number of elements?
-
A.
Height is log(n)
-
B.
Height is n
-
C.
Height is n log(n)
-
D.
Height is constant
Solution
The height of a max-heap is log(n) because it is a complete binary tree, where n is the number of elements.
Correct Answer:
A
— Height is log(n)
Learn More →
Q. In a min-heap, which of the following is true about the parent and child nodes?
-
A.
Parent is always greater than children
-
B.
Parent is always less than children
-
C.
Parent can be equal to children
-
D.
None of the above
Solution
In a min-heap, the parent node is always less than or equal to its children, ensuring the minimum element is at the root.
Correct Answer:
B
— Parent is always less than children
Learn More →
Q. In a priority queue implemented with a binary heap, what happens when the maximum element is removed?
-
A.
The last element is placed at the root
-
B.
The root is replaced with the minimum element
-
C.
The heap is restructured
-
D.
Both A and C
Solution
When the maximum element is removed, the last element is placed at the root, and then the heap is restructured to maintain the heap property.
Correct Answer:
D
— Both A and C
Learn More →
Q. What is the primary advantage of using a binary heap over an unsorted array for implementing a priority queue?
-
A.
Faster insertion
-
B.
Faster deletion
-
C.
Better memory usage
-
D.
Easier implementation
Solution
A binary heap allows for faster deletion of the maximum (or minimum) element compared to an unsorted array, where deletion would take O(n) time.
Correct Answer:
B
— Faster deletion
Learn More →
Q. What is the time complexity of building a heap from an array of n elements?
-
A.
O(n)
-
B.
O(log n)
-
C.
O(n log n)
-
D.
O(n^2)
Solution
Building a heap from an array can be done in O(n) time using the heapify process.
Correct Answer:
A
— O(n)
Learn More →
Q. What is the time complexity of extracting the minimum element from a min-heap?
-
A.
O(1)
-
B.
O(log n)
-
C.
O(n)
-
D.
O(n log n)
Solution
Extracting the minimum element from a min-heap requires restructuring the heap, which takes O(log n) time.
Correct Answer:
B
— O(log n)
Learn More →
Q. What is the worst-case time complexity for deleting the minimum element from a min-heap?
-
A.
O(1)
-
B.
O(log n)
-
C.
O(n)
-
D.
O(n log n)
Solution
Deleting the minimum element from a min-heap involves removing the root and then re-heapifying, which takes O(log n) time.
Correct Answer:
B
— O(log n)
Learn More →
Q. Which data structure is commonly used to implement a priority queue?
-
A.
Array
-
B.
Linked List
-
C.
Binary Search Tree
-
D.
Heap
Solution
A priority queue is commonly implemented using a heap, as it allows for efficient insertion and deletion of the highest (or lowest) priority element.
Correct Answer:
D
— Heap
Learn More →
Q. Which of the following is NOT a valid operation for a priority queue?
-
A.
Insert
-
B.
Delete Min
-
C.
Get Min
-
D.
Sort
Solution
Sorting is not a direct operation of a priority queue; it can be achieved by repeatedly removing elements, but it is not a standard operation.
Correct Answer:
D
— Sort
Learn More →
Q. Which of the following statements about heaps is true?
-
A.
Heaps are always balanced
-
B.
Heaps can be implemented using arrays
-
C.
Heaps can only be binary
-
D.
Heaps are not complete binary trees
Solution
Heaps can be efficiently implemented using arrays, where the parent-child relationship can be easily calculated.
Correct Answer:
B
— Heaps can be implemented using arrays
Learn More →
Showing 1 to 12 of 12 (1 Pages)