Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a PriorityQueue of integers in Java.
DSA Java
PriorityQueue<Integer> pq = new PriorityQueue[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of angle brackets for generics.
Using curly braces or square brackets which are invalid here.
✗ Incorrect
In Java, to create a generic PriorityQueue, you use angle brackets <> after the class name.
2fill in blank
mediumComplete the code to add an element 10 to the priority queue pq.
DSA Java
pq.[1](10);
Attempts:
3 left
💡 Hint
Common Mistakes
Using insert or push which are not methods of PriorityQueue.
Using enqueue which is not a Java PriorityQueue method.
✗ Incorrect
In Java's PriorityQueue, the method to add elements is add().
3fill in blank
hardFix the error in the code to remove the highest priority element from pq.
DSA Java
int highest = pq.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using pop which is not a PriorityQueue method.
Using remove which throws exception if empty.
Using dequeue which is not a PriorityQueue method.
✗ Incorrect
poll() removes and returns the head of the PriorityQueue or returns null if empty.
4fill in blank
hardFill both blanks to check if pq is empty and get its size.
DSA Java
boolean empty = pq.[1](); int size = pq.[2]();
Attempts:
3 left
💡 Hint
Common Mistakes
Using length or count which are not PriorityQueue methods.
Confusing isEmpty() with size().
✗ Incorrect
isEmpty() checks if the queue is empty, size() returns the number of elements.
5fill in blank
hardFill all three blanks to create a max-heap PriorityQueue using a comparator.
DSA Java
PriorityQueue<Integer> maxPQ = new PriorityQueue<>([1], [2].reverseOrder()); maxPQ.[3](5);
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong constructor parameters.
Using wrong method to add elements.
Confusing initial capacity with element values.
✗ Incorrect
The constructor takes initial capacity (e.g., 10) and a comparator like Collections.reverseOrder(). Then add() inserts elements.
