Challenge - 5 Problems
Sorting Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Counting Sort on a small array
What is the output of the following C++ code that uses Counting Sort on the array [4, 2, 2, 8, 3, 3, 1]?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; void countingSort(vector<int>& arr) { int max_val = *max_element(arr.begin(), arr.end()); vector<int> count(max_val + 1, 0); for (int num : arr) { count[num]++; } int index = 0; for (int i = 0; i <= max_val; i++) { while (count[i] > 0) { arr[index++] = i; count[i]--; } } } int main() { vector<int> arr = {4, 2, 2, 8, 3, 3, 1}; countingSort(arr); for (int num : arr) { cout << num << " "; } return 0; }
Attempts:
2 left
💡 Hint
Counting Sort counts occurrences and then rebuilds the array in sorted order.
✗ Incorrect
Counting Sort counts how many times each number appears and then writes them back in order. So the sorted array is 1 2 2 3 3 4 8.
🧠 Conceptual
intermediate1:30remaining
Time Complexity of Comparison Based Sorting
Which of the following is the best known lower bound on the time complexity of any comparison based sorting algorithm for n elements?
Attempts:
2 left
💡 Hint
Think about the decision tree model for comparisons.
✗ Incorrect
Comparison based sorting algorithms cannot do better than O(n log n) in the worst case because of the number of comparisons needed to distinguish all permutations.
❓ Predict Output
advanced2:30remaining
Output of Radix Sort on integers
What is the output of the following C++ code that uses Radix Sort on the array [170, 45, 75, 90, 802, 24, 2, 66]?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; void countingSortForRadix(vector<int>& arr, int exp) { int n = arr.size(); vector<int> output(n); int count[10] = {0}; 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]; } void radixSort(vector<int>& arr) { int max_val = *max_element(arr.begin(), arr.end()); for (int exp = 1; max_val / exp > 0; exp *= 10) { countingSortForRadix(arr, exp); } } int main() { vector<int> arr = {170, 45, 75, 90, 802, 24, 2, 66}; radixSort(arr); for (int num : arr) { cout << num << " "; } return 0; }
Attempts:
2 left
💡 Hint
Radix Sort sorts numbers digit by digit starting from least significant digit.
✗ Incorrect
Radix Sort sorts the array by each digit from right to left, resulting in the sorted array: 2 24 45 66 75 90 170 802.
🧠 Conceptual
advanced1:30remaining
Which sorting algorithm is NOT comparison based?
Among the following sorting algorithms, which one does NOT rely on comparing elements to sort the array?
Attempts:
2 left
💡 Hint
Counting Sort uses counting frequencies instead of comparisons.
✗ Incorrect
Counting Sort sorts by counting the number of occurrences of each value, not by comparing elements.
🧠 Conceptual
expert2:00remaining
Why can't comparison based sorting be faster than O(n log n)?
Why is it impossible for any comparison based sorting algorithm to have a worst-case time complexity better than O(n log n)?
Attempts:
2 left
💡 Hint
Think about how many different ways n elements can be arranged.
✗ Incorrect
The number of permutations of n elements is n!. To distinguish all permutations, a comparison based sorting algorithm must have at least log2(n!) comparisons, which is O(n log n).