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 Comparison Based Sorting Algorithm
What is the output of the following JavaScript code that uses a comparison based sorting method?
DSA Javascript
const arr = [5, 3, 8, 1, 2]; arr.sort((a, b) => a - b); console.log(arr.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Remember that the sort method with (a, b) => a - b sorts numbers in ascending order.
✗ Incorrect
The sort method compares elements and arranges them from smallest to largest, so the output is sorted ascending.
❓ Predict Output
intermediate2:00remaining
Output of Non Comparison Based Sorting Algorithm
What is the output of the following JavaScript code that uses a non comparison based sorting method (counting sort) on a small array?
DSA Javascript
function countingSort(arr) {
const max = Math.max(...arr);
const count = new Array(max + 1).fill(0);
for (const num of arr) count[num]++;
const sorted = [];
for (let i = 0; i <= max; i++) {
while (count[i] > 0) {
sorted.push(i);
count[i]--;
}
}
return sorted;
}
const arr = [4, 2, 2, 8, 3, 3, 1];
const sortedArr = countingSort(arr);
console.log(sortedArr.join(' -> ') + ' -> null');Attempts:
2 left
💡 Hint
Counting sort counts occurrences and outputs sorted values in order.
✗ Incorrect
Counting sort outputs elements in ascending order by counting their frequency.
🧠 Conceptual
advanced2:00remaining
Time 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 have a lower bound of O(n log n). Non comparison based algorithms like counting sort can run in O(n) if input range is limited.
❓ Predict Output
advanced2:00remaining
Output of Non Comparison Based Sorting Algorithm (Radix Sort)
What is the output of the following JavaScript code that uses a non comparison based sorting method (radix sort)?
DSA Javascript
function radixSort(arr) {
const max = Math.max(...arr);
let exp = 1;
while (Math.floor(max / exp) > 0) {
const output = new Array(arr.length).fill(0);
const count = new Array(10).fill(0);
for (let i = 0; i < arr.length; i++) {
const index = Math.floor(arr[i] / exp) % 10;
count[index]++;
}
for (let i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (let i = arr.length - 1; i >= 0; i--) {
const index = Math.floor(arr[i] / exp) % 10;
output[count[index] - 1] = arr[i];
count[index]--;
}
arr = output;
exp *= 10;
}
return arr;
}
const arr = [170, 45, 75, 90, 802, 24, 2, 66];
console.log(radixSort(arr).join(' -> ') + ' -> null');Attempts:
2 left
💡 Hint
This is a correct implementation of LSD radix sort using counting sort per digit.
✗ Incorrect
The code correctly implements radix sort (non-comparison based) and outputs the sorted array.
🚀 Application
expert2:00remaining
Choosing Sorting Algorithm for Large Dataset with Small Range
You have a large dataset of 10 million integers, but all values are between 0 and 1000. Which sorting algorithm is the best choice for fastest sorting?
Attempts:
2 left
💡 Hint
Consider the input range and how it affects non comparison based sorting.
✗ Incorrect
Counting sort is ideal for large datasets with small value ranges because it sorts in linear time O(n + k), where k is the range size.