Challenge - 5 Problems
Sorting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Sorting and Searching in a Sorted Array
What is the output of the following JavaScript code that sorts an array and then searches for a value using binary search?
DSA Javascript
function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) return mid;
else if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
const nums = [5, 3, 8, 4, 2];
nums.sort((a, b) => a - b);
const index = binarySearch(nums, 4);
console.log(nums);
console.log(index);Attempts:
2 left
💡 Hint
Remember that Array.sort with a compare function sorts numbers ascending, and binary search returns the index of the target in the sorted array.
✗ Incorrect
The array is sorted to [2, 3, 4, 5, 8]. The binary search looks for 4 and finds it at index 2 (0-based).
🧠 Conceptual
intermediate1:30remaining
Why Sorting Helps in Finding Duplicates
Why does sorting an array first make it easier to find duplicate elements?
Attempts:
2 left
💡 Hint
Think about how duplicates would look if the array is sorted.
✗ Incorrect
When the array is sorted, all duplicates are placed next to each other. This makes it easy to check neighbors to find duplicates.
❓ Predict Output
advanced2:00remaining
Output of Merging Two Sorted Arrays
What is the output of the following JavaScript code that merges two sorted arrays into one sorted array?
DSA Javascript
function mergeSortedArrays(arr1, arr2) {
const merged = [];
let i = 0, j = 0;
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) {
merged.push(arr1[i]);
i++;
} else {
merged.push(arr2[j]);
j++;
}
}
while (i < arr1.length) merged.push(arr1[i++]);
while (j < arr2.length) merged.push(arr2[j++]);
return merged;
}
const a = [1, 3, 5];
const b = [2, 4, 6];
console.log(mergeSortedArrays(a, b));Attempts:
2 left
💡 Hint
The function merges two sorted arrays by comparing elements one by one.
✗ Incorrect
The function compares elements from both arrays and pushes the smaller one, resulting in a fully sorted merged array.
🔧 Debug
advanced1:30remaining
Identify the Error in Sorting Objects by Property
What error will the following code produce when trying to sort an array of objects by their 'age' property?
DSA Javascript
const people = [{name: 'Alice', age: 25}, {name: 'Bob', age: 20}];
people.sort((a, b) => a.age > b.age);
console.log(people);Attempts:
2 left
💡 Hint
Array.sort compare function must return a number, not a boolean.
✗ Incorrect
The compare function returns true or false, which is converted to 1 or 0, causing incorrect sorting behavior.
🚀 Application
expert2:00remaining
Using Sorting to Optimize Interval Scheduling
Given a list of intervals with start and end times, which sorting strategy helps to find the maximum number of non-overlapping intervals?
Attempts:
2 left
💡 Hint
Choosing intervals that finish earliest leaves room for more intervals later.
✗ Incorrect
Sorting by end time ascending allows greedy selection of intervals that finish earliest, maximizing the count of non-overlapping intervals.