Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to perform a comparison-based sort using Array.Sort.
DSA C#
int[] numbers = {5, 3, 8, 1};
Array.[1](numbers);
foreach (int num in numbers) {
Console.Write(num + " ");
}Attempts:
3 left
💡 Hint
Common Mistakes
Using LINQ methods like OrderBy which return new sequences instead of sorting in place.
Using incorrect method names like SortBy or Order.
✗ Incorrect
Array.Sort is the built-in method in C# for comparison-based sorting of arrays.
2fill in blank
mediumComplete the code to perform a non-comparison based sort using Counting Sort logic for integers.
DSA C#
int[] arr = {4, 2, 2, 8, 3, 3, 1};
int max = arr.Max();
int[] count = new int[max + 1];
foreach (int num in arr) {
count[[1]]++;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the array name or max as index instead of the current number.
Incrementing the wrong array.
✗ Incorrect
In Counting Sort, we increment the count at the index equal to the number itself.
3fill in blank
hardFix the error in the code to correctly print the sorted array after Counting Sort.
DSA C#
int[] count = {0, 1, 2, 1};
for (int i = 0; i < count.Length; i++) {
for (int j = 0; j < count[[1]]; j++) {
Console.Write(i + " ");
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'j' as index which is the inner loop counter.
Using 'count' or '0' as index which causes errors.
✗ Incorrect
The inner loop should run count[i] times to print the number i that many times.
4fill in blank
hardFill both blanks to complete the code that builds a dictionary comprehension filtering numbers greater than 3.
DSA C#
var result = new Dictionary<int, int>(); foreach (var num in numbers) { if (num [1] 3) { result[num] = num [2] 1; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than instead of greater than in the condition.
Subtracting 1 instead of adding 1.
✗ Incorrect
We check if num is greater than 3 and then add 1 to num for the dictionary value.
5fill in blank
hardFill all three blanks to complete the LINQ expression that selects keys in uppercase where values are positive.
DSA C#
var filtered = data.Where(kv => kv.Value [1] 0) .ToDictionary(kv => kv.Key.[2](), kv => kv.[3]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the filter.
Using ToLower instead of ToUpper for keys.
Using Key instead of Value for the dictionary values.
✗ Incorrect
We filter values greater than 0, convert keys to uppercase, and keep values as is.
