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 code that counts the number of recursive calls in merge sort for an array of size 4?
DSA Python
def merge_sort_count(arr, count=[0]): count[0] += 1 if len(arr) <= 1: return arr mid = len(arr) // 2 left = merge_sort_count(arr[:mid], count) right = merge_sort_count(arr[mid:], count) return sorted(left + right) arr = [4, 3, 2, 1] count = [0] merge_sort_count(arr, count) print(count[0])
Attempts:
2 left
💡 Hint
Count how many times the function is called including base cases.
✗ Incorrect
Merge sort splits the array recursively until single elements. For size 4, calls are: 1 (whole), 2 (two halves), 4 (single elements) totaling 7 calls.
🧠 Conceptual
intermediateRecurrence Relation for Binary Search
What is the correct recurrence relation for the time complexity of binary search on an array of size n?
Attempts:
2 left
💡 Hint
Binary search halves the array each step and does constant work.
✗ Incorrect
Binary search divides the problem size by 2 each step and does constant time work to compare, so T(n) = T(n/2) + O(1).
❓ Predict Output
advancedOutput of Recursive Function with Divide and Conquer
What is the output of this code that calculates the sum of elements using divide and conquer?
DSA Python
def divide_sum(arr): if len(arr) == 1: return arr[0] mid = len(arr) // 2 left_sum = divide_sum(arr[:mid]) right_sum = divide_sum(arr[mid:]) return left_sum + right_sum print(divide_sum([1, 2, 3, 4, 5]))
Attempts:
2 left
💡 Hint
Sum all elements by splitting the array recursively.
✗ Incorrect
The function splits the array recursively and sums left and right parts. The total sum of [1,2,3,4,5] is 15.
🔧 Debug
advancedIdentify the Error in Recurrence Calculation Code
What error does the following code produce when calculating Fibonacci numbers using divide and conquer?
DSA Python
def fib(n): if n <= 1: return n return fib(n-1) + fib(n-2) print(fib('5'))
Attempts:
2 left
💡 Hint
Check the type of input passed to the function.
✗ Incorrect
Passing a string '5' causes TypeError when trying to compare or subtract integers.
🧠 Conceptual
expertMastering the Master Theorem Case
Given the recurrence T(n) = 4T(n/2) + n^2, what is the time complexity using the Master Theorem?
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, this matches case 2 of Master Theorem, so T(n) = O(n^2 log n).
