0
0
DSA Goprogramming~20 mins

Why Binary Search and What Sorted Order Gives You in DSA Go - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Binary Search on Sorted Array
What is the output of this Go code that performs binary search on a sorted array to find the index of number 7?
DSA Go
package main
import "fmt"
func binarySearch(arr []int, target int) int {
    left, right := 0, len(arr)-1
    for left <= right {
        mid := left + (right-left)/2
        if arr[mid] == target {
            return mid
        } else if arr[mid] < target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return -1
}
func main() {
    arr := []int{1, 3, 5, 7, 9, 11}
    fmt.Println(binarySearch(arr, 7))
}
A2
B3
C-1
D4
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and the array is sorted.
🧠 Conceptual
intermediate
1:30remaining
Why Must the Array Be Sorted for Binary Search?
Why is it necessary for the array to be sorted before applying binary search?
ABecause binary search compares the target with the middle element to decide which half to search next, which only works if the array is sorted.
BBecause binary search only works on arrays with unique elements.
CBecause binary search can only find the first element in any array.
DBecause binary search requires the array to be sorted in descending order only.
Attempts:
2 left
💡 Hint
Think about how binary search decides which half to discard.
Predict Output
advanced
2:00remaining
Result of Binary Search on Unsorted Array
What is the output of this Go code that performs binary search on an unsorted array to find the index of number 7?
DSA Go
package main
import "fmt"
func binarySearch(arr []int, target int) int {
    left, right := 0, len(arr)-1
    for left <= right {
        mid := left + (right-left)/2
        if arr[mid] == target {
            return mid
        } else if arr[mid] < target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return -1
}
func main() {
    arr := []int{9, 1, 5, 3, 11, 7}
    fmt.Println(binarySearch(arr, 7))
}
A-1
B2
C3
D0
Attempts:
2 left
💡 Hint
Binary search assumes sorted order; what happens if the array is not sorted?
🔧 Debug
advanced
2:30remaining
Identify the Bug in Binary Search Implementation
What is the bug in this binary search code that causes it to fail on some inputs?
DSA Go
package main
import "fmt"
func binarySearch(arr []int, target int) int {
    left, right := 0, len(arr)-1
    for left < right {
        mid := (left + right) / 2
        if arr[mid] == target {
            return mid
        } else if arr[mid] < target {
            left = mid + 1
        } else {
            right = mid
        }
    }
    return -1
}
func main() {
    arr := []int{1, 3, 5, 7, 9}
    fmt.Println(binarySearch(arr, 9))
}
AThe mid calculation should be left + (right-left)/2 to avoid overflow.
BThe loop condition should be left <= right, not left < right.
CThe initial right should be len(arr)-1, not len(arr).
DThe function should return left instead of -1 when not found.
Attempts:
2 left
💡 Hint
Check the range of indices used for right boundary.
🧠 Conceptual
expert
2:00remaining
Why Binary Search is Faster Than Linear Search on Large Sorted Data
Why does binary search perform faster than linear search on large sorted arrays?
ABecause binary search uses recursion and recursion is always faster than loops.
BBecause linear search requires the array to be sorted, which adds overhead.
CBecause binary search only works on arrays with fewer elements.
DBecause binary search reduces the search space by half each step, leading to logarithmic time complexity, while linear search checks each element one by one.
Attempts:
2 left
💡 Hint
Think about how many elements each algorithm checks as the array size grows.