Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a min heap using PriorityQueue.
DSA Java
PriorityQueue<ListNode> minHeap = new PriorityQueue<>([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using a comparator that reverses order, creating a max heap instead of min heap.
Using addition instead of subtraction in comparator.
✗ Incorrect
The comparator (a, b) -> a.val - b.val creates a min heap based on node values.
2fill in blank
mediumComplete the code to add the first node of each list to the min heap.
DSA Java
for (ListNode node : lists) { if (node != null) { minHeap.[1](node); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using poll() which removes elements instead of adding.
Using peek() which only views the top element.
✗ Incorrect
The offer() method adds elements to the PriorityQueue (min heap).
3fill in blank
hardFix the error in extracting the smallest node from the min heap.
DSA Java
while (!minHeap.isEmpty()) { ListNode smallest = minHeap.[1](); // process smallest }
Attempts:
3 left
💡 Hint
Common Mistakes
Using peek() which does not remove the element.
Using removeLast() or pop() which are not PriorityQueue methods.
✗ Incorrect
poll() removes and returns the smallest element from the min heap.
4fill in blank
hardFill both blanks to add the next node of the smallest node to the min heap if it exists.
DSA Java
if (smallest.[1] != null) { minHeap.[2](smallest.next); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using prev instead of next to get the next node.
Using addLast() which is not a PriorityQueue method.
✗ Incorrect
smallest.next accesses the next node; offer() adds it to the min heap.
5fill in blank
hardFill all three blanks to build the merged linked list by connecting nodes.
DSA Java
ListNode dummy = new ListNode(0); ListNode current = dummy; while (!minHeap.isEmpty()) { ListNode smallest = minHeap.poll(); current.[1] = smallest; current = current.[2]; if (smallest.next != null) { minHeap.[3](smallest.next); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using prev instead of next for linking nodes.
Using remove instead of offer to add nodes to min heap.
✗ Incorrect
current.next connects to smallest; current moves to current.next; offer() adds next node to min heap.
