0% found this document useful (0 votes)
3 views2 pages

PQSample Questions

The document contains sample questions related to priority queues and heaps, including operations like Enqueue, Dequeue, and ChangeKey. It discusses the complexities of various data structures for implementing priority queues and compares their performance for different operations. Additionally, it provides algorithms for finding the kth largest number in a Max-heap and analyzes the worst-case complexities for various operations.

Uploaded by

Nour H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

PQSample Questions

The document contains sample questions related to priority queues and heaps, including operations like Enqueue, Dequeue, and ChangeKey. It discusses the complexities of various data structures for implementing priority queues and compares their performance for different operations. Additionally, it provides algorithms for finding the kth largest number in a Max-heap and analyzes the worst-case complexities for various operations.

Uploaded by

Nour H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Sample Questions: PQ

Q2) Consider the following heap:

6 7

8 20 17 12

70 9

(a) Draw the array representation of this heap.


(b) Draw the heap after an Enqueue(5) operation is performed.
(c) how will the heap array look like after we apply the following operation: dequeue(0)
(d) Draw the heap after the key value 6 is changed to 25. Apply the change to the original heap
not to the one you drew in Part (b)

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

 Data structure to use is Priority Queue


Sample Questions: PQ

 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)

Heap O(logN) O(logN)

Unordered DLL O(1) O(N)


8. Assuming that you have N numbers stored in a Max-heap, describe an efficient
algorithm that finds the kth largest number (for example, if k=3, the k th largest number is
the 3rd largest number, and so on). Then give the worst-case asymptotic complexity of
your algorithm in terms of N and k. It will be OK if your algorithm modifies the heap
We can find the kth largest element by performing the DeleteMax (Dequeue)
operation k times. The asymptotic complexity is O(klogN).

You might also like