0
0
DSA Goprogramming~10 mins

Floor and Ceil in Sorted Array 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 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'
Ahigh
Barr[mid]
Ctarget
Dlow
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[mid] with low or high instead of target.
Using '<' instead of '<=' which misses exact matches.
2fill in blank
medium

Complete 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'
Aarr[mid]
Btarget
Clow
Dhigh
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[mid] with low or high instead of target.
Using '>' instead of '>=' which misses exact matches.
3fill in blank
hard

Fix 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'
Alow == high
Blow < high
Clow >= high
Dlow <= high
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.
4fill in blank
hard

Fill 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'
Atarget
Bmid + 1
Cmid - 1
Darr[mid]
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison variable instead of target.
Updating high incorrectly causing infinite loops.
5fill in blank
hard

Fill 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'
Atarget
Bmid + 1
Cmid - 1
Dtarget + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Returning indices instead of values.
Incorrectly updating low or high causing infinite loops.