0
0
DSA Javascriptprogramming~20 mins

Merge Sort Algorithm in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Merge Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Merge Sort on a small array
What is the output of the following JavaScript code that uses merge sort on the array [4, 1, 3, 2]?
DSA Javascript
function merge(left, right) {
  let result = [];
  let i = 0, j = 0;
  while (i < left.length && j < right.length) {
    if (left[i] < right[j]) {
      result.push(left[i]);
      i++;
    } else {
      result.push(right[j]);
      j++;
    }
  }
  return result.concat(left.slice(i)).concat(right.slice(j));
}

function mergeSort(arr) {
  if (arr.length <= 1) return arr;
  const mid = Math.floor(arr.length / 2);
  const left = mergeSort(arr.slice(0, mid));
  const right = mergeSort(arr.slice(mid));
  return merge(left, right);
}

console.log(mergeSort([4, 1, 3, 2]));
A[1, 2, 3, 4]
B[4, 3, 2, 1]
C[1, 3, 2, 4]
D[2, 1, 3, 4]
Attempts:
2 left
💡 Hint
Think about how merge sort splits and merges arrays in sorted order.
🧠 Conceptual
intermediate
1:00remaining
Time Complexity of Merge Sort
What is the time complexity of the merge sort algorithm in the average and worst cases?
AO(n)
BO(n^2)
CO(log n)
DO(n log n)
Attempts:
2 left
💡 Hint
Consider how many times the array is split and merged.
🔧 Debug
advanced
2:00remaining
Identify the error in this merge function
What error will occur when running this merge function used in merge sort?
DSA Javascript
function merge(left, right) {
  let result = [];
  let i = 0, j = 0;
  while (i < left.length && j < right.length) {
    if (left[i] < right[j]) {
      result.push(left[i]);
      i++;
    } else {
      result.push(right[j]);
      j++;
    }
  }
  return result.concat(left.slice(i)).concat(right.slice(j));
}
ASyntaxError: Unexpected token
BTypeError: Cannot read property of undefined
CNo error, returns sorted array
DInfinite loop
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
🚀 Application
advanced
2:30remaining
Using Merge Sort to Sort Objects by a Key
Given an array of objects [{name: 'Bob', age: 25}, {name: 'Alice', age: 30}, {name: 'Eve', age: 22}], which option correctly sorts the array by age using merge sort?
DSA Javascript
function merge(left, right) {
  let result = [];
  let i = 0, j = 0;
  while (i < left.length && j < right.length) {
    if (left[i].age < right[j].age) {
      result.push(left[i]);
      i++;
    } else {
      result.push(right[j]);
      j++;
    }
  }
  return result.concat(left.slice(i)).concat(right.slice(j));
}

function mergeSort(arr) {
  if (arr.length <= 1) return arr;
  const mid = Math.floor(arr.length / 2);
  const left = mergeSort(arr.slice(0, mid));
  const right = mergeSort(arr.slice(mid));
  return merge(left, right);
}

const people = [{name: 'Bob', age: 25}, {name: 'Alice', age: 30}, {name: 'Eve', age: 22}];
console.log(mergeSort(people));
A[{"name":"Eve","age":22},{"name":"Alice","age":30},{"name":"Bob","age":25}]
B[{"name":"Bob","age":25},{"name":"Eve","age":22},{"name":"Alice","age":30}]
C[{"name":"Eve","age":22},{"name":"Bob","age":25},{"name":"Alice","age":30}]
D[{"name":"Alice","age":30},{"name":"Bob","age":25},{"name":"Eve","age":22}]
Attempts:
2 left
💡 Hint
Check the comparison inside the merge function uses the age property.
🧠 Conceptual
expert
1:30remaining
Space Complexity of Merge Sort
What is the space complexity of the merge sort algorithm and why?
AO(n) because merge sort requires extra space to hold merged arrays
BO(1) because merge sort sorts in place without extra space
CO(log n) because only recursive call stack uses space
DO(n log n) because it creates new arrays at each recursion level
Attempts:
2 left
💡 Hint
Think about the temporary arrays created during merging.