Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the min heap for merging sorted lists.
DSA C#
var minHeap = new SortedSet<(int val, int listIndex, int elementIndex)>[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] instead of parentheses () to initialize the SortedSet.
Using curly braces {} which is invalid here.
✗ Incorrect
The min heap is initialized as an empty SortedSet with parentheses ().
2fill in blank
mediumComplete the code to add the first element of each list to the min heap.
DSA C#
for (int i = 0; i < lists.Length; i++) { if (lists[i] != null && lists[i].Count > 0) { minHeap.Add((lists[i][0], i, 0)); } } while (minHeap.Count > 0) { var current = minHeap.Min; minHeap.Remove(current); result.Add(current.val); int nextIndex = current.elementIndex + 1; if (nextIndex < lists[current.listIndex].Count) { minHeap.Add([1]); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping listIndex and elementIndex in the tuple.
Using current.val instead of the next element's value.
✗ Incorrect
We add the next element from the same list to the min heap with its value, list index, and element index.
3fill in blank
hardFix the error in the comparison logic for the SortedSet to maintain min heap order.
DSA C#
var minHeap = new SortedSet<(int val, int listIndex, int elementIndex)>(Comparer<(int, int, int)>.Create((a, b) => {
if (a.val != b.val) return a.val - b.val;
if (a.listIndex != b.listIndex) return a.listIndex - b.listIndex;
return [1];
}));Attempts:
3 left
💡 Hint
Common Mistakes
Reversing the subtraction causing descending order.
Comparing listIndex again instead of elementIndex.
✗ Incorrect
To maintain consistent ordering, compare elementIndex ascending as the last tie-breaker.
4fill in blank
hardFill both blanks to complete the loop that merges k sorted lists using the min heap.
DSA C#
while (minHeap.Count > 0) { var current = minHeap.Min; minHeap.Remove(current); result.Add([1]); int nextIndex = current.elementIndex + 1; if (nextIndex < lists[[2]].Count) { minHeap.Add((lists[current.listIndex][nextIndex], current.listIndex, nextIndex)); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Adding current.elementIndex instead of current.val to result.
Using nextIndex instead of current.listIndex to access lists.
✗ Incorrect
Add the current value to result and check the next element in the same list using current.listIndex.
5fill in blank
hardFill all three blanks to complete the method that merges k sorted lists using a min heap.
DSA C#
public List<int> MergeKSortedLists(List<int>[] lists) {
var result = new List<int>();
var minHeap = new SortedSet<(int val, int listIndex, int elementIndex)>(Comparer<(int, int, int)>.Create((a, b) => {
if (a.val != b.val) return a.val - b.val;
if (a.listIndex != b.listIndex) return a.listIndex - b.listIndex;
return a.elementIndex - b.elementIndex;
}));
for (int i = 0; i < lists.Length; i++) {
if (lists[i] != null && lists[i].Count > 0) {
minHeap.Add((lists[i][0], i, 0));
}
}
while (minHeap.Count > 0) {
var current = minHeap.Min;
minHeap.Remove(current);
result.Add([1]);
int nextIndex = current.elementIndex + 1;
if (nextIndex < lists[[2]].Count) {
minHeap.Add((lists[[3]][nextIndex], current.listIndex, nextIndex));
}
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using nextIndex instead of current.listIndex to access lists.
Adding elementIndex instead of val to the result.
✗ Incorrect
Add current.val to result, use current.listIndex to check the next element and add it to the min heap.
