0
0
DSA Javascriptprogramming~20 mins

Why Sorting Matters and How It Unlocks Other Algorithms in DSA Javascript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sorting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A[5, 3, 8, 4, 2]\n-1
B[2, 3, 4, 5, 8]\n3
C[5, 3, 8, 4, 2]\n3
D[2, 3, 4, 5, 8]\n2
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.
🧠 Conceptual
intermediate
1:30remaining
Why Sorting Helps in Finding Duplicates
Why does sorting an array first make it easier to find duplicate elements?
ABecause sorting reverses the array, which helps in finding duplicates.
BBecause sorting groups equal elements together, so duplicates appear next to each other making them easy to spot.
CBecause sorting changes the values of elements to unique ones.
DBecause sorting removes duplicates automatically.
Attempts:
2 left
💡 Hint
Think about how duplicates would look if the array is sorted.
Predict Output
advanced
2: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));
A[1, 2, 3, 4, 5, 6]
B[1, 3, 5, 2, 4, 6]
C[2, 4, 6, 1, 3, 5]
D[1, 2, 4, 3, 5, 6]
Attempts:
2 left
💡 Hint
The function merges two sorted arrays by comparing elements one by one.
🔧 Debug
advanced
1: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);
AThe array is sorted incorrectly because the compare function returns a boolean instead of a number.
BSyntaxError due to missing return statement in compare function.
CThe array is sorted correctly by age ascending.
DTypeError because 'age' is undefined.
Attempts:
2 left
💡 Hint
Array.sort compare function must return a number, not a boolean.
🚀 Application
expert
2: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?
ASort intervals by their duration (end - start) in descending order.
BSort intervals by their start time in ascending order.
CSort intervals by their end time in ascending order.
DSort intervals by their start time in descending order.
Attempts:
2 left
💡 Hint
Choosing intervals that finish earliest leaves room for more intervals later.