0
0
DSA Goprogramming~20 mins

Binary Search vs Linear Search Real Cost Difference in DSA Go - Compare & Choose

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Search Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Linear Search on Sorted Array
What is the output of the following Go code that performs a linear search for the value 7 in a sorted array?
DSA Go
package main
import "fmt"
func linearSearch(arr []int, target int) int {
    for i, v := range arr {
        if v == target {
            return i
        }
    }
    return -1
}
func main() {
    arr := []int{1, 3, 5, 7, 9, 11}
    index := linearSearch(arr, 7)
    fmt.Println(index)
}
A4
B2
C-1
D3
Attempts:
2 left
💡 Hint
Linear search returns the index of the first matching element.
Predict Output
intermediate
2:00remaining
Output of Binary Search on Sorted Array
What is the output of the following Go code that performs a binary search for the value 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) / 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}
    index := binarySearch(arr, 7)
    fmt.Println(index)
}
A3
B2
C-1
D4
Attempts:
2 left
💡 Hint
Binary search divides the array and finds the target efficiently.
🧠 Conceptual
advanced
2:00remaining
Time Complexity Comparison
Which statement correctly compares the average time complexity of linear search and binary search on a sorted array of size n?
ALinear search is O(n), binary search is O(log n)
BLinear search is O(log n), binary search is O(n)
CBoth linear and binary search are O(n)
DBoth linear and binary search are O(log n)
Attempts:
2 left
💡 Hint
Binary search cuts the search space in half each step.
🚀 Application
advanced
2:00remaining
Choosing Search Method for Unsorted Data
You have a large unsorted list of numbers and need to find if a target number exists. Which search method is best and why?
ABinary search, because it is always faster
BNeither, use a hash table instead
CLinear search, because binary search requires sorted data
DBinary search after sorting the list every time you search
Attempts:
2 left
💡 Hint
Binary search needs sorted data to work correctly.
🔧 Debug
expert
2:00remaining
Bug in Binary Search Implementation
What error does the following Go code produce when searching for 12 in the array? Identify the output or error.
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) / 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}
    index := binarySearch(arr, 12)
    fmt.Println(index)
}
A-1
Bruntime error: index out of range
C3
D0
Attempts:
2 left
💡 Hint
Check the initial value of high and array indexing.