0
0
DSA Goprogramming~10 mins

Binary Search on Answer Technique 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 binary search boundaries for the answer.

DSA Go
low, high := 0, [1]
Drag options to blanks, or click blank then click option'
Alen(arr)
Bmath.MaxInt64
C1000000
Darr[len(arr)-1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using array length as upper bound which may be too small
Using last element of array which may not represent max answer
2fill in blank
medium

Complete the code to calculate the middle value in binary search safely.

DSA Go
mid := low + (high - [1]) / 2
Drag options to blanks, or click blank then click option'
Amid
Blow
Chigh
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using (low + high) / 2 directly which can overflow
Using wrong variable in subtraction
3fill in blank
hard

Fix the error in the condition to check if mid is a valid answer.

DSA Go
if canAchieve(arr, [1]) {
Drag options to blanks, or click blank then click option'
Amid
Bhigh
Clow
Dmid + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Passing high or low instead of mid
Passing mid + 1 which is not the current candidate
4fill in blank
hard

Fill both blanks to update the binary search boundaries correctly.

DSA Go
if canAchieve(arr, mid) {
    high = [1]
} else {
    low = [2]
}
Drag options to blanks, or click blank then click option'
Amid
Bmid + 1
Cmid - 1
Dlow
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to mid instead of mid + 1 causing infinite loop
Setting high to mid - 1 which may skip valid answers
5fill in blank
hard

Fill all three blanks to return the final answer after binary search.

DSA Go
for low < high {
    mid := low + (high - low) / 2
    if canAchieve(arr, mid) {
        high = [1]
    } else {
        low = [2]
    }
}
return [3]
Drag options to blanks, or click blank then click option'
Amid
Blow
Cmid + 1
Dhigh
Attempts:
3 left
💡 Hint
Common Mistakes
Returning mid instead of low after loop
Incorrectly updating boundaries causing infinite loop