Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a min heap using Java's PriorityQueue.
DSA Java
PriorityQueue<Integer> minHeap = new PriorityQueue<>(); // Creates a [1] heap by defaultAttempts:
3 left
💡 Hint
Common Mistakes
Trying to pass a comparator for min heap is unnecessary.
Using a max heap comparator by mistake.
✗ Incorrect
In Java, PriorityQueue with no arguments creates a min heap by default — the smallest element is always at the top.
2fill in blank
mediumComplete the code to create a max heap using Java's PriorityQueue with a comparator.
DSA Java
PriorityQueue<Integer> maxHeap = new PriorityQueue<>([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using Comparator.naturalOrder() creates a min heap instead.
Passing null causes default min heap behavior.
✗ Incorrect
To create a max heap, use Comparator.reverseOrder() to reverse the natural ascending order.
3fill in blank
hardFix the error in this code snippet that tries to add elements to a max heap.
DSA Java
PriorityQueue<Integer> maxHeap = new PriorityQueue<>([1]); maxHeap.add(10); maxHeap.add(20); maxHeap.add(15);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Comparator.naturalOrder() results in min heap behavior.
Passing null defaults to min heap.
✗ Incorrect
Using Comparator.reverseOrder() correctly creates a max heap; other options create min heap or cause errors.
4fill in blank
hardFill both blanks to create a min heap and add an element to it.
DSA Java
PriorityQueue<Integer> heap = new PriorityQueue<>(); // [1] heap heap.[2](5);
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a comparator for min heap unnecessarily.
Using remove() instead of add() to insert.
✗ Incorrect
No-argument PriorityQueue creates a min heap. Use add() to insert an element.
5fill in blank
hardFill all three blanks to create a max heap, add an element, and remove the top element.
DSA Java
PriorityQueue<Integer> heap = new PriorityQueue<>([1]); heap.[2](10); int top = heap.[3]();
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove() instead of poll() can throw exceptions if empty.
Using naturalOrder() creates min heap.
✗ Incorrect
Comparator.reverseOrder() creates max heap; add() inserts; poll() removes top safely.
