0
0
DSA Goprogramming~20 mins

Binary Search Recursive Approach in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Search Recursive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Recursive Binary Search on Sorted Array
What is the output of the following Go code that performs a recursive binary search for the value 7 in the array?
DSA Go
package main
import "fmt"

func binarySearch(arr []int, low, high, target int) int {
    if low > high {
        return -1
    }
    mid := low + (high-low)/2
    if arr[mid] == target {
        return mid
    } else if arr[mid] > target {
        return binarySearch(arr, low, mid-1, target)
    } else {
        return binarySearch(arr, mid+1, high, target)
    }
}

func main() {
    arr := []int{1, 3, 5, 7, 9, 11}
    result := binarySearch(arr, 0, len(arr)-1, 7)
    fmt.Println(result)
}
A2
B-1
C3
D4
Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and the function returns the index of the target if found.
Predict Output
intermediate
2:00remaining
Result of Recursive Binary Search When Target Not Present
What will be printed when searching for 8 in the sorted array using the recursive binary search function below?
DSA Go
package main
import "fmt"

func binarySearch(arr []int, low, high, target int) int {
    if low > high {
        return -1
    }
    mid := low + (high-low)/2
    if arr[mid] == target {
        return mid
    } else if arr[mid] > target {
        return binarySearch(arr, low, mid-1, target)
    } else {
        return binarySearch(arr, mid+1, high, target)
    }
}

func main() {
    arr := []int{2, 4, 6, 10, 12}
    result := binarySearch(arr, 0, len(arr)-1, 8)
    fmt.Println(result)
}
A-1
B2
C3
D0
Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
🔧 Debug
advanced
2:00remaining
Identify the Error in Recursive Binary Search Implementation
What error will occur when running this recursive binary search code?
DSA Go
package main
import "fmt"

func binarySearch(arr []int, low, high, target int) int {
    if low > high {
        return -1
    }
    mid := low + (high-low)/2
    if arr[mid] == target {
        return mid
    } else if arr[mid] > target {
        return binarySearch(arr, low, mid-1, target)
    } else {
        return binarySearch(arr, mid+1, high, target)
    }
}

func main() {
    arr := []int{1, 2, 3, 4, 5}
    result := binarySearch(arr, 0, len(arr)-1, 3)
    fmt.Println(result)
}
AInfinite recursion causing stack overflow
BIndex out of range runtime panic
CCompiles but prints 0 always
DReturns -1 even if target is present
Attempts:
2 left
💡 Hint
Check the base case condition carefully.
🧠 Conceptual
advanced
1:00remaining
Understanding Recursive Binary Search Time Complexity
What is the time complexity of the recursive binary search algorithm on a sorted array of size n?
AO(log n)
BO(1)
CO(n log n)
DO(n)
Attempts:
2 left
💡 Hint
Each recursive call halves the search space.
🚀 Application
expert
3:00remaining
Find the Index of First Occurrence Using Recursive Binary Search
Given a sorted array with duplicate elements, which recursive binary search modification will correctly find the first occurrence index of the target value 5? Consider the array: [1, 2, 5, 5, 5, 7, 9]
DSA Go
package main
import "fmt"

func firstOccurrence(arr []int, low, high, target int) int {
    if low > high {
        return -1
    }
    mid := low + (high-low)/2
    if arr[mid] == target {
        if mid == 0 || arr[mid-1] < target {
            return mid
        } else {
            return firstOccurrence(arr, low, mid-1, target)
        }
    } else if arr[mid] < target {
        return firstOccurrence(arr, mid+1, high, target)
    } else {
        return firstOccurrence(arr, low, mid-1, target)
    }
}

func main() {
    arr := []int{1, 2, 5, 5, 5, 7, 9}
    result := firstOccurrence(arr, 0, len(arr)-1, 5)
    fmt.Println(result)
}
A4
B2
C-1
D3
Attempts:
2 left
💡 Hint
Check if the mid element is the first occurrence or if you need to search left.