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
Consider the following Java method that counts the number of recursive calls made during a merge sort on an array of size n. What is the output when calling
countMergeSortCalls(8)?DSA Java
public class MergeSortCounter { static int count = 0; public static int countMergeSortCalls(int n) { if (n <= 1) { count++; return count; } count++; countMergeSortCalls(n / 2); countMergeSortCalls(n / 2); return count; } public static void main(String[] args) { System.out.println(countMergeSortCalls(8)); } }
Attempts:
2 left
💡 Hint
Think about how many times the array is split until size 1 and how many calls are made at each level.
✗ Incorrect
The merge sort splits the array into halves recursively until size 1. For n=8, the total calls are 2*n - 1 = 15.
🧠 Conceptual
intermediateMaster Theorem Case Identification
Given the recurrence relation
T(n) = 3T(n/4) + n, which case of the Master Theorem applies?Attempts:
2 left
💡 Hint
Calculate log base b of a and compare f(n) with n^{log_b(a)}.
✗ Incorrect
Here, a=3, b=4, so log_b(a) = log_4(3) ≈ 0.792. Since f(n) = n = n^1, and 1 > 0.792, f(n) grows faster than n^{log_b(a)}. But since f(n) is polynomially larger, it fits Case 3.
🔧 Debug
advancedIdentify the Error in Recurrence Implementation
This Java method attempts to compute the time complexity T(n) = 2T(n/2) + n using recursion and returns the total cost. What error will occur when running
computeTime(0)?DSA Java
public class Recurrence { public static int computeTime(int n) { if (n == 1) { return 1; } return 2 * computeTime(n / 2) + n; } public static void main(String[] args) { System.out.println(computeTime(0)); } }
Attempts:
2 left
💡 Hint
Check the base case and what happens when n=0.
✗ Incorrect
When n=0, the base case (n==1) is never reached. The method calls computeTime(0/2) = computeTime(0) infinitely, causing StackOverflowError.
🚀 Application
advancedCalculate Number of Subproblems in Divide and Conquer
A divide and conquer algorithm splits a problem of size n into 5 subproblems each of size n/3, and does linear work outside the recursive calls. How many subproblems are created at the third level of recursion?
Attempts:
2 left
💡 Hint
Number of subproblems at level k is a^k where a is the number of subproblems per call.
✗ Incorrect
At each level, the number of subproblems multiplies by 5. At level 3, total subproblems = 5^3 = 125.
🧠 Conceptual
expertExact Solution of Recurrence Using Recursion Tree
Given the recurrence
T(n) = 4T(n/2) + n^2, what is the exact asymptotic solution for T(n)?Attempts:
2 left
💡 Hint
Use Master Theorem: a=4, b=2, f(n)=n^2, compare f(n) with n^{log_b a}.
✗ Incorrect
Here, a=4, b=2, so log_b(a) = log_2(4) = 2. f(n) = n^2 matches n^{log_b(a)}. This is Master Theorem Case 2, so T(n) = Θ(n^2 log n).
