0
0
DSA Goprogramming~10 mins

Binary Search Iterative Approach in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the middle index in the binary search loop.

DSA Go
mid := low [1] (high - low) / 2
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes incorrect mid calculation.
2fill in blank
medium

Complete the code to update the low index when the target is greater than the middle element.

DSA Go
if arr[mid] < target {
    low = [1]
}
Drag options to blanks, or click blank then click option'
Amid
Bhigh
Cmid + 1
Dlow + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to mid causes infinite loop if mid doesn't change.
3fill in blank
hard

Fix the error in the loop condition to correctly run binary search while low is less than or equal to high.

DSA Go
for low [1] high {
    // binary search steps
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' misses checking when low equals high.
4fill in blank
hard

Fill both blanks to update the high index when the target is less than the middle element.

DSA Go
if target < arr[mid] {
    high = [1]
} else {
    low = [2]
}
Drag options to blanks, or click blank then click option'
Amid - 1
Bmid + 1
Chigh - 1
Dlow + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using high - 1 or low + 1 incorrectly for both updates.
5fill in blank
hard

Fill all three blanks to complete the binary search function that returns the index of target or -1 if not found.

DSA Go
func binarySearch(arr []int, target int) int {
    low, high := 0, len(arr) - 1
    for low [1] high {
        mid := low + (high - low) / 2
        if arr[mid] == target {
            return [2]
        } else if arr[mid] < target {
            low = [3]
        } else {
            high = mid - 1
        }
    }
    return -1
}
Drag options to blanks, or click blank then click option'
A<=
Bmid
Cmid + 1
Dmid - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Returning low or high instead of mid; wrong loop condition; wrong low update.