Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the floor value index in a sorted array.
DSA Go
func floorIndex(arr []int, target int) int {
low, high := 0, len(arr)-1
result := -1
for low <= high {
mid := low + (high - low) / 2
if arr[mid] <= [1] {
result = mid
low = mid + 1
} else {
high = mid - 1
}
}
return result
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[mid] with low or high instead of target.
Using '<' instead of '<=' which misses exact matches.
✗ Incorrect
We compare arr[mid] with the target to find the floor value index.
2fill in blank
mediumComplete the code to find the ceil value index in a sorted array.
DSA Go
func ceilIndex(arr []int, target int) int {
low, high := 0, len(arr)-1
result := -1
for low <= high {
mid := low + (high - low) / 2
if arr[mid] >= [1] {
result = mid
high = mid - 1
} else {
low = mid + 1
}
}
return result
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[mid] with low or high instead of target.
Using '>' instead of '>=' which misses exact matches.
✗ Incorrect
We compare arr[mid] with the target to find the ceil value index.
3fill in blank
hardFix the error in the binary search loop condition to avoid infinite loops.
DSA Go
func findFloor(arr []int, target int) int {
low, high := 0, len(arr)-1
result := -1
for [1] {
mid := low + (high - low) / 2
if arr[mid] <= target {
result = mid
low = mid + 1
} else {
high = mid - 1
}
}
return result
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using low < high causes missing the last element check.
Using low == high causes the loop to run only once.
✗ Incorrect
The loop should continue while low is less than or equal to high to cover all elements.
4fill in blank
hardFill both blanks to complete the function that returns floor and ceil indices for a target.
DSA Go
func floorAndCeil(arr []int, target int) (int, int) {
floor := -1
ceil := -1
low, high := 0, len(arr)-1
for low <= high {
mid := low + (high - low) / 2
if arr[mid] == target {
return mid, mid
} else if arr[mid] < [1] {
floor = mid
low = mid + 1
} else {
ceil = mid
high = [2]
}
}
return floor, ceil
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison variable instead of target.
Updating high incorrectly causing infinite loops.
✗ Incorrect
Compare arr[mid] with target; update high to mid - 1 when arr[mid] > target.
5fill in blank
hardFill all three blanks to create a function that returns floor and ceil values (not indices) or -1 if not found.
DSA Go
func floorAndCeilValues(arr []int, target int) (int, int) {
floor := -1
ceil := -1
low, high := 0, len(arr)-1
for low <= high {
mid := low + (high - low) / 2
if arr[mid] == [1] {
return arr[mid], arr[mid]
} else if arr[mid] < target {
floor = arr[mid]
low = [2]
} else {
ceil = arr[mid]
high = [3]
}
}
return floor, ceil
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning indices instead of values.
Incorrectly updating low or high causing infinite loops.
✗ Incorrect
Compare arr[mid] with target; update low to mid + 1 if arr[mid] < target; update high to mid - 1 if arr[mid] > target.