Challenge - 5 Problems
Sorting Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Counting Sort on a Small Array
What is the output of the following Python code that uses Counting Sort on the array [4, 2, 2, 8, 3, 3, 1]?
DSA Python
def counting_sort(arr): max_val = max(arr) count = [0] * (max_val + 1) for num in arr: count[num] += 1 sorted_arr = [] for i, c in enumerate(count): sorted_arr.extend([i] * c) return sorted_arr arr = [4, 2, 2, 8, 3, 3, 1] print(counting_sort(arr))
Attempts:
2 left
💡 Hint
Counting sort counts how many times each number appears and then reconstructs the sorted list.
✗ Incorrect
Counting sort counts the frequency of each number and then outputs them in order. The input array has duplicates, so the output includes all duplicates sorted.
🧠 Conceptual
intermediateTime 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?
Attempts:
2 left
💡 Hint
Think about the minimum number of comparisons needed to distinguish all permutations.
✗ Incorrect
Comparison based sorting algorithms cannot do better than O(n log n) in the worst case because they must distinguish between n! permutations.
❓ Predict Output
advancedOutput of Radix Sort on a List of Integers
What is the output of the following Python code that uses Radix Sort on the list [170, 45, 75, 90, 802, 24, 2, 66]?
DSA Python
def counting_sort_for_radix(arr, exp): n = len(arr) output = [0] * n count = [0] * 10 for i in range(n): index = (arr[i] // exp) % 10 count[index] += 1 for i in range(1, 10): count[i] += count[i - 1] i = n - 1 while i >= 0: index = (arr[i] // exp) % 10 output[count[index] - 1] = arr[i] count[index] -= 1 i -= 1 for i in range(n): arr[i] = output[i] def radix_sort(arr): max_val = max(arr) exp = 1 while max_val // exp > 0: counting_sort_for_radix(arr, exp) exp *= 10 arr = [170, 45, 75, 90, 802, 24, 2, 66] radix_sort(arr) print(arr)
Attempts:
2 left
💡 Hint
Radix sort sorts numbers digit by digit starting from least significant digit.
✗ Incorrect
Radix sort sorts the array by processing digits from least significant to most significant, resulting in a fully sorted array.
🧠 Conceptual
advancedWhich Sorting Algorithm is Non Comparison Based?
Which of the following sorting algorithms does NOT use comparisons between elements to sort the array?
Attempts:
2 left
💡 Hint
Non comparison based sorting uses counting or digit extraction instead of comparing elements.
✗ Incorrect
Counting Sort counts the occurrences of each element and sorts based on counts without comparing elements.
🧠 Conceptual
expertWhy Can't Comparison Based Sorting Beat 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 and how comparisons help identify the correct order.
✗ Incorrect
The decision tree model shows that to distinguish all n! permutations, at least log2(n!) comparisons are needed, which is O(n log n).
