Challenge - 5 Problems
Binary Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Binary Search on Sorted Array
What is the output of this Go code that performs binary search to find the index of number 7 in the sorted array?
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 indices start at 0 and 7 is the fourth element.
✗ Incorrect
The array is [1,3,5,7,9,11]. The number 7 is at index 3 (0-based). The binary search returns this index.
❓ Predict Output
intermediateBinary Search Result When Target Not Present
What will be the output of this Go code when searching for number 8 in the sorted array?
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{2, 4, 6, 10, 12} fmt.Println(binarySearch(arr, 8)) }
Attempts:
2 left
💡 Hint
If the target is not found, binary search returns -1.
✗ Incorrect
The number 8 is not in the array, so the function returns -1.
🧠 Conceptual
advancedDivide and Conquer Principle in Binary Search
Which statement best describes how binary search uses the divide and conquer approach?
Attempts:
2 left
💡 Hint
Think about how binary search reduces the search space by half each step.
✗ Incorrect
Binary search splits the array into two halves and continues searching only in the half where the target can be, ignoring the other half.
🔧 Debug
advancedIdentify the Bug in Binary Search Implementation
What error will this Go code produce when running binary search, and why?
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, 2, 3, 4, 5} fmt.Println(binarySearch(arr, 6)) }
Attempts:
2 left
💡 Hint
Check the initial value of 'right' and how it relates to array indices.
✗ Incorrect
The variable 'right' is set to len(arr), which is out of bounds for indexing. Accessing arr[mid] when mid equals len(arr) causes a runtime error.
🚀 Application
expertBinary Search on Rotated Sorted Array
Given a rotated sorted array, which option correctly finds the index of target 3 using a modified binary search?
DSA Go
package main import "fmt" func searchRotated(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 } if arr[left] <= arr[mid] { if arr[left] <= target && target < arr[mid] { right = mid - 1 } else { left = mid + 1 } } else { if arr[mid] < target && target <= arr[right] { left = mid + 1 } else { right = mid - 1 } } } return -1 } func main() { arr := []int{4, 5, 6, 7, 0, 1, 2, 3} fmt.Println(searchRotated(arr, 3)) }
Attempts:
2 left
💡 Hint
Remember the array is rotated; index 7 holds the number 3.
✗ Incorrect
The target 3 is at index 7 in the rotated array. The modified binary search correctly finds and returns this index.
