Challenge - 5 Problems
Divide and Conquer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Merge Sort Call Count
What is the output of the following JavaScript code that counts the number of recursive calls in merge sort for an array of length 4?
DSA Javascript
let count = 0; function mergeSort(arr) { count++; 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); } 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++]); } else { result.push(right[j++]); } } return result.concat(left.slice(i)).concat(right.slice(j)); } mergeSort([4, 3, 2, 1]); console.log(count);
Attempts:
2 left
💡 Hint
Count how many times mergeSort is called including the base cases.
✗ Incorrect
The mergeSort function is called once for the whole array, then recursively for each half until arrays of length 1. For length 4, calls are: 1 (whole), 2 (two halves), 4 (four single elements), total 7 calls.
🧠 Conceptual
intermediateRecurrence Relation for Binary Search
What is the correct recurrence relation for the time complexity T(n) of binary search on an array of size n?
Attempts:
2 left
💡 Hint
Binary search splits the array into half and does constant work each step.
✗ Incorrect
Binary search divides the problem size by 2 each time and does a constant amount of work to compare the middle element, so T(n) = T(n/2) + O(1).
🔧 Debug
advancedIdentify the Error in Recurrence Calculation
Consider the following recurrence relation code snippet in JavaScript to calculate T(n) = 2T(n/2) + n. What error will occur when calling recurrence(1)?
DSA Javascript
function recurrence(n) {
if (n === 1) return 1;
return 2 * recurrence(n / 2) + n;
}
console.log(recurrence(1));Attempts:
2 left
💡 Hint
Check the base case and how n changes in recursive calls.
✗ Incorrect
The base case returns 1 when n is exactly 1. Since n is divided by 2 each time, it reaches 1 exactly for powers of two, so no infinite recursion occurs and the function returns 1.
❓ Predict Output
advancedOutput of Recursive Fibonacci with Memoization
What is the output of the following JavaScript code that uses divide and conquer with memoization to compute fibonacci(5)?
DSA Javascript
function fibonacci(n, memo = {}) {
if (n <= 1) return n;
if (memo[n]) return memo[n];
memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo);
return memo[n];
}
console.log(fibonacci(5));Attempts:
2 left
💡 Hint
Recall the Fibonacci sequence: 0,1,1,2,3,5,...
✗ Incorrect
The function returns the 5th Fibonacci number (0-based index), which is 5.
🧠 Conceptual
expertMastering the Master Theorem Case
Given the recurrence relation T(n) = 4T(n/2) + n^2, which case of the Master Theorem applies and what is the asymptotic complexity?
Attempts:
2 left
💡 Hint
Compare n^log_b(a) with f(n) where a=4, b=2, f(n)=n^2.
✗ Incorrect
Here, a=4, b=2, so n^log_b(a) = n^2. Since f(n) = n^2, it matches case 2 of the Master Theorem, so T(n) = Θ(n^2 log n).
