0
0
DSA Goprogramming~20 mins

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

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 for Existing Element
What is the output of the following Go code that performs an iterative binary search for the number 7 in a sorted array?
DSA Go
package main
import "fmt"

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

func main() {
    arr := []int{1, 3, 5, 7, 9, 11}
    fmt.Println(binarySearch(arr, 7))
}
A-1
B2
C3
D4
Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and the array is sorted.
Predict Output
intermediate
2:00remaining
Output of Binary Search for Non-Existing Element
What is the output of the following Go code that performs an iterative binary search for the number 7 in a sorted array?
DSA Go
package main
import "fmt"

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

func main() {
    arr := []int{2, 4, 6, 8, 10, 12}
    fmt.Println(binarySearch(arr, 7))
}
A4
B3
C2
D-1
Attempts:
2 left
💡 Hint
Check if the target exists in the array and note its index.
Predict Output
advanced
2:00remaining
Output of Binary Search with Duplicate Elements
What is the output of the following Go code that performs an iterative binary search for the number 5 in a sorted array with duplicates?
DSA Go
package main
import "fmt"

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

func main() {
    arr := []int{1, 3, 5, 5, 5, 7, 9}
    fmt.Println(binarySearch(arr, 5))
}
A2
B3
C4
D-1
Attempts:
2 left
💡 Hint
Binary search may return any index where the target is found.
🧠 Conceptual
advanced
1:30remaining
Why Use Iterative Binary Search Instead of Recursive?
Which of the following is the main advantage of using an iterative approach for binary search over a recursive one?
AIterative approach uses less memory by avoiding function call stack overhead.
BIterative approach is easier to write and understand than recursive.
CIterative approach can handle unsorted arrays.
DIterative approach always finds the first occurrence of duplicates.
Attempts:
2 left
💡 Hint
Think about memory usage and function calls.
🔧 Debug
expert
2:30remaining
Identify the Bug in Binary Search Implementation
What error will the following Go code produce when running binary search on the array [1, 2, 3, 4, 5] searching for 6?
DSA Go
package main
import "fmt"

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

func main() {
    arr := []int{1, 2, 3, 4, 5}
    fmt.Println(binarySearch(arr, 6))
}
Aruntime error: index out of range
Breturns 2 (correct index)
Creturns -1 (not found)
Dcompilation error
Attempts:
2 left
💡 Hint
Check the initial value of 'high' and array indexing.