Challenge - 5 Problems
Divide and Conquer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Recursive Merge Sort Call Count
Consider the following TypeScript function that counts how many times the mergeSort function is called for an array of length n. What is the output when calling
mergeSortCount([3,1,4,1,5])?DSA Typescript
function mergeSortCount(arr: number[], count = {calls: 0}): number {
count.calls++;
if (arr.length <= 1) return count.calls;
const mid = Math.floor(arr.length / 2);
mergeSortCount(arr.slice(0, mid), count);
mergeSortCount(arr.slice(mid), count);
return count.calls;
}
console.log(mergeSortCount([3,1,4,1,5]));Attempts:
2 left
💡 Hint
Think about how many times the function splits the array and calls itself recursively.
✗ Incorrect
The mergeSort function calls itself recursively a total of 9 times for an array of length 5 (2n - 1 = 9 calls).
🧠 Conceptual
intermediate1:30remaining
Recurrence Relation for Binary Search
What is the correct recurrence relation for the time complexity T(n) of the binary search algorithm on an array of size n?
Attempts:
2 left
💡 Hint
Binary search splits the array into half and does a constant amount of work each step.
✗ Incorrect
Binary search divides the problem size by 2 each time and does constant work to compare the middle element, so the recurrence is T(n) = T(n/2) + O(1).
🔧 Debug
advanced2:00remaining
Identify the Error in Recurrence Calculation
The following TypeScript function attempts to compute the number of operations for a divide and conquer algorithm with recurrence T(n) = 2T(n/2) + n. What error causes incorrect output?
DSA Typescript
function operationsCount(n: number): number {
if (n <= 1) return 1;
return 2 * operationsCount(Math.floor(n / 2)) + n;
}
console.log(operationsCount(8));Attempts:
2 left
💡 Hint
Check how the recursive calls reduce n and if they reach the base case.
✗ Incorrect
Using n / 2 with floating point division can cause incorrect results for inputs where n/2 is not integer, as the recurrence assumes integer division.
🚀 Application
advanced1:30remaining
Calculate Time Complexity from Recurrence
Given the recurrence relation T(n) = 3T(n/3) + n, what is the time complexity in Big O notation?
Attempts:
2 left
💡 Hint
Use the Master Theorem to solve the recurrence.
✗ Incorrect
Here a=3, b=3, f(n)=n. Since f(n) = Theta(n^{log_b a}), time complexity is O(n log n).
❓ Predict Output
expert2:30remaining
Output of Recursive Function with Custom Recurrence
What is the output of the following TypeScript code?
DSA Typescript
function customRecurrence(n: number): number {
if (n < 1) return 0;
return customRecurrence(Math.floor(n / 3)) + customRecurrence(Math.floor(2 * n / 3)) + n;
}
console.log(customRecurrence(9));Attempts:
2 left
💡 Hint
Trace the recursive calls carefully and sum the results.
✗ Incorrect
The function splits n into floor(n/3) and floor(2n/3), sums their results plus n. For n=9, the total is 33.