📖 Scenario: Imagine you have a list of book titles sorted alphabetically on a shelf. You want to find if a certain book is there quickly without checking every book one by one.
🎯 Goal: You will create a sorted list of book titles, set a target book to find, use binary search to check if the book is in the list, and print the result.
📋 What You'll Learn
Create a sorted slice of strings called books with these exact titles: "Algorithms", "Data Structures", "Go Programming", "Networking", "Operating Systems"
Create a string variable called target and set it to "Go Programming"
Write a function called binarySearch that takes a slice of strings and a target string, and returns a boolean indicating if the target is found using binary search
Use the binarySearch function to check if target is in books
Print "Found" if the book is in the list, otherwise print "Not Found"
💡 Why This Matters
🌍 Real World
Binary search is used in many apps and systems to quickly find data like names, files, or numbers when the data is sorted.
💼 Career
Understanding binary search and sorted data is important for software developers to write efficient search features and optimize performance.
Progress0 / 4 steps
1
Create the sorted list of books
Create a slice of strings called books with these exact titles in this order: "Algorithms", "Data Structures", "Go Programming", "Networking", "Operating Systems"
DSA Go
Hint
Use books := []string{...} with the exact titles in the given order.
2
Set the target book to find
Create a string variable called target and set it to "Go Programming"
DSA Go
Hint
Use target := "Go Programming" to set the book you want to find.
3
Write the binary search function
Write a function called binarySearch that takes a slice of strings called arr and a string target, and returns a boolean. Use binary search logic to check if target is in arr. Use variables low, high, and mid for indexes.
DSA Go
Hint
Use a loop with low and high indexes, calculate mid, compare arr[mid] with target, and adjust low or high accordingly.
4
Use binary search and print the result
Use the binarySearch function to check if target is in books. Print "Found" if true, otherwise print "Not Found".
DSA Go
Hint
Call binarySearch(books, target). Use fmt.Println("Found") if true, else fmt.Println("Not Found").
Practice
(1/5)
1. Why is binary search only applicable on a sorted list?
easy
A. Because it relies on dividing the list based on order to find elements quickly
B. Because it works only on lists with unique elements
C. Because it requires the list to be reversed
D. Because it compares every element one by one
Solution
Step 1: Understand binary search logic
Binary search splits the list into halves and decides which half to search next based on order.
Step 2: Importance of sorted order
Without sorted order, we cannot decide which half to discard, so binary search fails.
Final Answer:
Because it relies on dividing the list based on order to find elements quickly -> Option A
Quick Check:
Sorted list needed for binary search = Because it relies on dividing the list based on order to find elements quickly [OK]
Hint: Binary search needs order to skip half the list each step [OK]
Common Mistakes:
Thinking binary search works on unsorted lists
Believing binary search compares all elements
Assuming binary search needs unique elements
2. Which of the following Go code snippets correctly performs binary search on a sorted slice arr to find target?
easy
A. 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 - 1
}
}
return -1
B. for i := 0; i < len(arr); i++ {
if arr[i] == target {
return i
}
}
return -1
C. left, right := 0, len(arr)
for left < right {
mid := (left + right) / 2
if arr[mid] == target {
return mid
} else if arr[mid] > target {
left = mid + 1
} else {
right = mid - 1
}
}
return -1
D. left, right := 0, len(arr)-1
for left <= right {
mid := (left + right) / 2
if arr[mid] == target {
return mid
} else if arr[mid] > target {
right = mid - 1
} else {
left = mid - 1
}
}
return -1
Solution
Step 1: Check binary search loop and boundaries
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 - 1
}
}
return -1 correctly initializes left and right, uses <=, and updates left and right properly.
Step 2: Identify errors in other options
for i := 0; i < len(arr); i++ {
if arr[i] == target {
return i
}
}
return -1 is linear search, not binary. left, right := 0, len(arr)
for left < right {
mid := (left + right) / 2
if arr[mid] == target {
return mid
} else if arr[mid] > target {
left = mid + 1
} else {
right = mid - 1
}
}
return -1 has wrong boundary and updates. left, right := 0, len(arr)-1
for left <= right {
mid := (left + right) / 2
if arr[mid] == target {
return mid
} else if arr[mid] > target {
right = mid - 1
} else {
left = mid - 1
}
}
return -1 incorrectly updates left pointer.
Final Answer:
Option A code correctly implements binary search -> Option A
Quick Check:
Correct binary search syntax = 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 - 1
}
}
return -1 [OK]
3. What will be the output of the following Go code snippet?
arr := []int{2, 4, 6, 8, 10}
target := 6
left, right := 0, len(arr)-1
for left <= right {
mid := (left + right) / 2
if arr[mid] == target {
fmt.Println(mid)
break
} else if arr[mid] < target {
left = mid + 1
} else {
right = mid - 1
}
}
medium
A. 0
B. 3
C. 1
D. 2
Solution
Step 1: Trace binary search steps
Initial left=0, right=4, mid=2 (value 6). arr[mid] == target, so print 2 and break.
Step 2: Confirm output
The printed index is 2, which is the position of 6 in the slice.
Final Answer:
2 -> Option D
Quick Check:
Index of 6 in arr = 2 [OK]
Hint: Check mid index value matches target first [OK]
Common Mistakes:
Confusing index with value
Miscomputing mid index
Ignoring break after finding target
4. The following Go code attempts to perform binary search but does not work correctly. What is the bug?
arr := []int{1, 3, 5, 7, 9}
target := 7
left, right := 0, len(arr)-1
for left < right {
mid := (left + right) / 2
if arr[mid] == target {
fmt.Println(mid)
break
} else if arr[mid] < target {
left = mid + 1
} else {
right = mid - 1
}
}
medium
A. The array must be reversed before binary search
B. Loop condition should be left <= right instead of left < right
C. left and right pointers are updated incorrectly
D. mid calculation should be mid := (left + right + 1) / 2
Solution
Step 1: Analyze loop condition
The loop uses left < right, which misses the case when left == right, possibly skipping the last element.
Step 2: Correct loop condition
Changing to left <= right ensures all elements are checked, fixing the bug.
Final Answer:
Loop condition should be left <= right instead of left < right -> Option B
Quick Check:
Loop must cover all indices = Loop condition should be left <= right instead of left < right [OK]
Hint: Use <= in loop to include last element check [OK]
Common Mistakes:
Using < instead of <= in loop condition
Changing mid calculation unnecessarily
Thinking array must be reversed
5. Given a sorted slice arr := []int{1, 3, 5, 7, 9, 11, 13}, you want to find the smallest element greater than or equal to target = 8. Which modification to binary search helps achieve this?
hard
A. Use linear search instead of binary search
B. When arr[mid] > target, move left to mid + 1; else move right to mid - 1
C. When arr[mid] < target, move left to mid + 1; else move right to mid - 1 and track mid as potential answer
D. Reverse the array and perform binary search
Solution
Step 1: Understand the problem
We want the smallest element >= target, so we track candidates when arr[mid] >= target.
Step 2: Modify binary search accordingly
If arr[mid] < target, move left to mid + 1; else move right to mid - 1 and save mid as potential answer.
Final Answer:
When arr[mid] < target, move left to mid + 1; else move right to mid - 1 and track mid as potential answer -> Option C
Quick Check:
Track candidate when arr[mid] >= target = When arr[mid] < target, move left to mid + 1; else move right to mid - 1 and track mid as potential answer [OK]