Challenge - 5 Problems
Sorting Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Comparison Based Sorting Algorithm
What is the output of the following C# code that uses a comparison based sorting method (Bubble Sort) on the array?
DSA C#
int[] arr = {5, 3, 8, 4, 2}; for (int i = 0; i < arr.Length - 1; i++) { for (int j = 0; j < arr.Length - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } foreach (var num in arr) { Console.Write(num + " "); }
Attempts:
2 left
💡 Hint
Bubble sort repeatedly swaps adjacent elements if they are in wrong order.
✗ Incorrect
Bubble sort compares adjacent elements and swaps them if the first is greater than the second. After all passes, the array is sorted in ascending order.
❓ Predict Output
intermediateOutput of Non Comparison Based Sorting Algorithm
What is the output of the following C# code that uses a non comparison based sorting method (Counting Sort) on the array?
DSA C#
int[] arr = {4, 2, 2, 8, 3, 3, 1}; int max = 8; int[] count = new int[max + 1]; for (int i = 0; i < arr.Length; i++) { count[arr[i]]++; } int index = 0; for (int i = 0; i <= max; i++) { while (count[i] > 0) { arr[index++] = i; count[i]--; } } foreach (var num in arr) { Console.Write(num + " "); }
Attempts:
2 left
💡 Hint
Counting sort counts occurrences and places elements in order without comparisons.
✗ Incorrect
Counting sort counts how many times each number appears, then writes them back in order. The final sorted array is ascending.
🧠 Conceptual
advancedTime Complexity Difference
Which statement correctly describes the time complexity difference between comparison based and non comparison based sorting algorithms?
Attempts:
2 left
💡 Hint
Think about the theoretical lower bound for comparison based sorting.
✗ Incorrect
Comparison based sorting algorithms cannot be faster than O(n log n) in the worst case. Non comparison based algorithms like counting sort can run in O(n) if conditions are right.
🔧 Debug
advancedIdentify the Error in Non Comparison Based Sort Implementation
What error will the following C# code produce when trying to sort an array using a non comparison based sorting method (Radix Sort)?
DSA C#
int[] arr = {170, 45, 75, 90, 802, 24, 2, 66}; int max = arr.Max(); int exp = 1; int n = arr.Length; int[] output = new int[n]; while (max / exp > 0) { int[] count = new int[10]; for (int i = 0; i < n; i++) { count[(arr[i] / exp) % 10]++; } for (int i = 1; i < 10; i++) { count[i] += count[i - 1]; } for (int i = n - 1; i >= 0; i--) { output[count[(arr[i] / exp) % 10] - 1] = arr[i]; count[(arr[i] / exp) % 10]--; } for (int i = 0; i < n; i++) { arr[i] = output[i]; } exp *= 10; } foreach (var num in arr) { Console.Write(num + " "); }
Attempts:
2 left
💡 Hint
Check how the output array is indexed inside the loop.
✗ Incorrect
The code uses count[(arr[i] / exp) % 10] as index directly without subtracting 1, causing index out of range error.
🚀 Application
expertChoosing Sorting Algorithm for Large Range Data
You have a large dataset of integers ranging from 1 to 1,000,000,000. Which sorting approach is most efficient in terms of time and space?
Attempts:
2 left
💡 Hint
Consider the space requirements of non comparison based sorts for large ranges.
✗ Incorrect
Counting sort requires space proportional to the range, which is huge here. Radix sort is possible but slower than efficient comparison sorts for large ranges. QuickSort or MergeSort are better choices.
