PQSample Questions
PQSample Questions
6 7
8 20 17 12
70 9
Q2) General
1. A binary heap is not a good data structure for doing a search operation. Explain this
statement by briefly describing how the search can be done in a binary heap and specifying
the asymptotic complexity.
2. Suppose that you need to implement a priority queue in an application where the operation of
finding the item with the highest priority occurs much more frequently than the operations
that modify the queue. Which priority queue implementation would you choose? Briefly
justify your choice by comparing the relevant Big-O values of the reasonable options.
3. Suppose that you need to implement a priority queue in an application where the
ChangeKey operation occurs much more frequently than any other operation. Which data
structure will you choose for your implementation? Justify your choice by comparing the
running times of the reasonable options.
4. Suppose that you need to implement a priority queue in an application that performs a large
number of both Enqueue and Dequeue operations and your goal is avoiding any long run
times. Which data structure would you use? Justify your answer specifying the Big-O.
5. Suppose that you are implementing a server application that receives client requests and
processes them according to their importance (the most important request is processed first,
then the second most important request and so on). Given that the only two operations that
the application performs on the requests are storing them in this data structure and removing
them after they have been processed, which data structure would you use for storing these
requests? Give a specific implementation for this data structure and specify the asymptotic
complexities of both operations in your implementation
In can be implemented as Heap , Ordered Array, unordered Array , or ordered link list
Heap: Store and Remove both of order log(n)
Ordered Array: Store O(n) and Remove O(n) as it will require shifting in both
operations.
Ordered Link List: Store O(n) and Remove O(1)
Unordered Array: Enqueue is O(1) and Dequeue is O(n) because we will need a get min
function.
Heap is best Choice because we will need both operations.
6. Briefly describe how you can perform the FindMax operation in a balanced BST and how
you can perform the same operation in a Min-Heap. Analyze the worst-case running time of
this operation for each data structure. Your analysis should be based on estimating the
number of elements that have to be processed. To simplify your analysis, assume that the
heap is a complete binary tree implemented as an array.
FindMax in a balanced BST is done by starting from root and going to the right most
node of the BST as it will be the max O(log n) because it will go maximum by the
height of the tree which is O(log n) for balanced BST
FindMax in heap is order O(n) as you have to inspect all leaves of the heap. In case of
complete binary tree, the leaves will be (n+1)/2 , so the order will be O(n)
7. Compare the worst-case asymptotic complexities of the Insert and ExtractMin operations
for each of the following two implementations of a priority queue.
ExtractMin
Insert
(DeleteMin)