0
0
DSA Typescriptprogramming~20 mins

Divide and Conquer Strategy and Recurrence Relations in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Divide and Conquer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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]));
A9
B11
C7
D5
Attempts:
2 left
💡 Hint
Think about how many times the function splits the array and calls itself recursively.
🧠 Conceptual
intermediate
1: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?
AT(n) = T(n/2) + O(1)
BT(n) = 2T(n/2) + O(n)
CT(n) = T(n-1) + O(1)
DT(n) = T(n/2) + O(n)
Attempts:
2 left
💡 Hint
Binary search splits the array into half and does a constant amount of work each step.
🔧 Debug
advanced
2: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));
AThe function should multiply n by 2 instead of adding n.
BThe base case should be n === 0 instead of n <= 1.
CThe function uses floating point division instead of integer division, causing incorrect results.
DThe function should return n instead of 1 in the base case.
Attempts:
2 left
💡 Hint
Check how the recursive calls reduce n and if they reach the base case.
🚀 Application
advanced
1: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?
AO(n)
BO(n^2)
CO(log n)
DO(n log n)
Attempts:
2 left
💡 Hint
Use the Master Theorem to solve the recurrence.
Predict Output
expert
2: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));
A27
B33
C31
D29
Attempts:
2 left
💡 Hint
Trace the recursive calls carefully and sum the results.