Challenge - 5 Problems
Binary Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)) }
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and the array is sorted.
✗ Incorrect
The number 7 is at index 3 in the sorted array [1,3,5,7,9,11]. Binary search finds it and returns 3.
🧠 Conceptual
intermediate1:30remaining
Why Must the Array Be Sorted for Binary Search?
Why is it necessary for the array to be sorted before applying binary search?
Attempts:
2 left
💡 Hint
Think about how binary search decides which half to discard.
✗ Incorrect
Binary search relies on the order of elements to eliminate half of the search space each time. Without sorting, it cannot decide which half to discard.
❓ Predict Output
advanced2: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)) }
Attempts:
2 left
💡 Hint
Binary search assumes sorted order; what happens if the array is not sorted?
✗ Incorrect
The array is not sorted, so binary search cannot reliably find the target. It returns -1 indicating not found.
🔧 Debug
advanced2: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)) }
Attempts:
2 left
💡 Hint
Check the range of indices used for right boundary.
✗ Incorrect
Using right = len(arr) causes mid to sometimes be out of range, leading to runtime errors or incorrect results.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about how many elements each algorithm checks as the array size grows.
✗ Incorrect
Binary search halves the search space each step, so it takes fewer steps as the array grows, unlike linear search which checks every element.