PQ Heaps
PQ Heaps
February 2025
Instructions
• Please use the following questions as practice questions for learning about Priority Queues and Heaps.
• The questions with * next to them should be attempted during the lab sessions, and your solutions
must be uploaded on moodlenew. Note that your submissions will not be evaluated, but will be used as
a mark of your attendance. We will filter out all the submissions that are not from the lab workstations.
So do not use your laptops for submitting the programs.
1 Questions
1. * Implement a Priority Queue from scratch
Implement all methods of the class signature for a priority queue below using heaps as taught in class.
Avoid using LLMs for this problem :) since this is the first non trivial data structure implementation
you have encountered in this course and so should try your best to complete this yourself.
Also try to use this class in remaining problems instead of java.utils.PriorityQueue .
1
4 /* *
5 * Constructor : Initializes an empty priority queue ( max heap ) .
6 * Time Complexity : O (1)
7 */
8 public PriorityQueue () ;
9
10 /* *
11 * Inserts a new key into the heap
12 * Time Complexity : O ( log N )
13 */
14 public void insert ( int key ) ;
15
16 /* *
17 * Extracts and returns the maximum element
18 * Time Complexity : O ( log N )
19 * @return the maximum value in the heap
20 * @throws Ille galS tateE xcep tion if the heap is empty
21 */
22 public int extractMax () ;
23
1
24 /* *
25 * Returns the maximum element without removing it .
26 * Time Complexity : O (1)
27 * @return the maximum value in the heap
28 * @throws Ille galS tateE xcep tion if the heap is empty
29 */
30 public int getMax () ;
31
32 /* *
33 * Deletes a specific key from the heap and maintains the heap property .
34 * Time Complexity : O ( N ) ( for searching ) + O ( log N ) ( for deletion )
35 * @param key the key to delete
36 * @throws I l l e ga l A rg u m e nt E x c ep t i o n if the key is not found
37 */
38 public void delete ( int key ) ;
39
40 /* *
41 * Updates the value of a specific key and maintains the heap property .
42 * Time Complexity : O ( N ) ( for searching ) + O ( log N ) ( for updation )
43 * @param oldKey the existing key in the heap
44 * @param newKey the new value to update the key with
45 * @throws I l l e ga l A rg u m e nt E x c ep t i o n if the key is not found
46 */
47 public void updateKey ( int oldKey , int newKey ) ;
48
49 /* *
50 * Builds a max heap from an unsorted array .
51 * Time Complexity : O ( N )
52 * @param array an array of integers to build the heap from
53 */
54 public void buildHeap ( int [] array ) ;
55
56 }
3. * K Smallest Elements
Implement a method ArrayList<Integer> kSmallestElements(int[] arr, int k) to find the k
smallest elements in an array of integers and return them (order of returned k elements can be arbi-
trary). (n >= k >= 1)
Example:
Input: arr = [3, 4, 5, 2, 10] , k = 3
Output: [3, 2, 5]
Time Complexity: O(N logk), where N is the size of the array.
2
4. Find the Median of an input stream
The median is the middle value in an ordered integer list. If the size of the list is even, there is no
middle value, and the median is the mean of the two middle values
Given an input stream of integers (in form of an array) arr, return an array med where medi =
M edian(arr[: i]), where arr[: i] is the subarray of elements upto index i.
Implement a method with signature ArrayList<Double> streamMed(int A[]).
Example:
Input: arr = [5 ,6, 9, 10, 1, 2]
Output: med = [5, 5.5, 6, 7.5, 6, 5.5]
• The number of soldiers in row i is less than the number of soldiers in row j.
• Both rows have the same number of soldiers and i < j.
Return the indices of the k weakest rows in the matrix ordered from weakest to strongest.
If the power is enough to yield sufficiently sweet cookies, that is, all cookies have sweetness ≥ k, then
return the minimum number of times the power has to be exploited, else return −1.
Example:
Input: mat = [[1, 1, 1], [1, 1, 0], [0, 0, 0]], k = 2
Output: [2, 1]
3
• String get() queries and returns the ith best location in O(log n) time complexity, where i is
the number of times this method has been invoked (including the current invocation). Assume
that i < number of stored locations = n.
8. Most Used Meeting Room
There are n rooms numbered from 0 to n − 1. You are given a 2D integer array meetings where
meetings[i] = [starti , endi ] means that a meeting will be held during the half-closed time interval
[starti , endi ) such that starti is unique ∀ i. The elements of meetings are sorted in increasing order
w.r.t. the starting times starti .
Meetings are allocated to rooms in the following manner:
• Each meeting will take place in the unused room with the lowest number.
• If there are no available rooms, the meeting will be delayed until a room becomes free. The
delayed meeting should have the same duration as the original meeting.
• When a room becomes unused, meetings that have an earlier original start time should be given
the room.
Return the number of the room that held the most meetings in O(k log n) time complexity where k
is the number of meetings. If there are multiple rooms, return the room with the lowest number.
Example:
Input: meetings = [[1, 5], [2, 4], [3, 5]], n = 2
Output: 1