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 Go code that counts the number of recursive calls made by a merge sort implementation on an array of size 4. What is the printed output?
DSA Go
package main import "fmt" var count int func mergeSort(arr []int) []int { count++ if len(arr) <= 1 { return arr } mid := len(arr) / 2 left := mergeSort(arr[:mid]) right := mergeSort(arr[mid:]) return merge(left, right) } func merge(left, right []int) []int { result := []int{} i, j := 0, 0 for i < len(left) && j < len(right) { if left[i] < right[j] { result = append(result, left[i]) i++ } else { result = append(result, right[j]) j++ } } result = append(result, left[i:]...) result = append(result, right[j:]...) return result } func main() { arr := []int{4, 3, 2, 1} mergeSort(arr) fmt.Println(count) }
Attempts:
2 left
💡 Hint
Count how many times mergeSort is called including the initial call and all recursive calls on subarrays.
✗ Incorrect
Merge sort splits the array into halves recursively until size 1. For size 4, calls are: mergeSort(4 elements), mergeSort(2 elements), mergeSort(1 element) twice, mergeSort(2 elements), mergeSort(1 element) twice. Total calls = 7.
🧠 Conceptual
intermediateRecurrence Relation for Binary Search
What is the correct recurrence relation for the time complexity T(n) of binary search on a sorted array of size n?
Attempts:
2 left
💡 Hint
Binary search divides the problem size by half each step and does constant work per step.
✗ Incorrect
Binary search splits the array into half each time and does constant time comparison, so T(n) = T(n/2) + O(1).
❓ Predict Output
advancedOutput of Recursive Function with Divide and Conquer
What is the output of the following Go program?
DSA Go
package main import "fmt" func f(n int) int { if n <= 1 { return 1 } return f(n/2) + f(n/2) + n } func main() { fmt.Println(f(4)) }
Attempts:
2 left
💡 Hint
Trace the recursive calls and sum their results carefully.
✗ Incorrect
f(1) = 1. f(2) = f(1) + f(1) + 2 = 1 + 1 + 2 = 4. f(4) = f(2) + f(2) + 4 = 4 + 4 + 4 = 12.
🧠 Conceptual
advancedMaster Theorem Case Identification
Given the recurrence T(n) = 3T(n/4) + n, which case of the Master Theorem applies and what is the asymptotic complexity?
Attempts:
2 left
💡 Hint
Compare n with n^{log_b a} where a=3 and b=4.
✗ Incorrect
Here a=3, b=4, log_4 3 ≈ 0.792. f(n)=n = Θ(n^{1}) which is asymptotically larger than n^{log_4 3}. Since f(n) = Θ(n^{log_4 3} * log n), Case 2 applies and T(n) = Θ(n log n).
🔧 Debug
expertIdentify the Error in Recursive Fibonacci Implementation
What error will the following Go code produce when executed?
DSA Go
package main import "fmt" func fib(n int) int { if n == 0 { return 0 } if n == 1 { return 1 } return fib(n-1) + fib(n-3) } func main() { fmt.Println(fib(4)) }
Attempts:
2 left
💡 Hint
Check the recursive calls and base cases carefully.
✗ Incorrect
The function calls fib(n-3) without a base case for n < 0, causing infinite recursion and stack overflow.
