0
0
DSA Goprogramming~20 mins

Floor and Ceil in Sorted Array in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Floor and Ceil Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find floor and ceil of 7 in a sorted array
Given the sorted array [1, 3, 5, 7, 9], what is the floor and ceil of 7?
DSA Go
arr := []int{1, 3, 5, 7, 9}
num := 7
floor, ceil := -1, -1
for _, v := range arr {
    if v <= num {
        floor = v
    }
    if v >= num && ceil == -1 {
        ceil = v
    }
}
fmt.Printf("Floor: %d, Ceil: %d", floor, ceil)
AFloor: 5, Ceil: 7
BFloor: 7, Ceil: 9
CFloor: 7, Ceil: 7
DFloor: 5, Ceil: 9
Attempts:
2 left
💡 Hint
Floor is the greatest element less than or equal to the number; ceil is the smallest element greater than or equal to the number.
Predict Output
intermediate
2:00remaining
Floor and ceil for a number not in the array
What is the floor and ceil of 6 in the sorted array [1, 3, 5, 7, 9]?
DSA Go
arr := []int{1, 3, 5, 7, 9}
num := 6
floor, ceil := -1, -1
for _, v := range arr {
    if v <= num {
        floor = v
    }
    if v >= num && ceil == -1 {
        ceil = v
    }
}
fmt.Printf("Floor: %d, Ceil: %d", floor, ceil)
AFloor: 7, Ceil: 9
BFloor: 5, Ceil: 7
CFloor: 5, Ceil: 6
DFloor: 6, Ceil: 7
Attempts:
2 left
💡 Hint
Floor is the greatest element less than or equal to the number; ceil is the smallest element greater than or equal to the number.
🧠 Conceptual
advanced
1:30remaining
Behavior when number is smaller than all elements
If the number is smaller than all elements in a sorted array, what will be the floor and ceil?
AFloor: -1 (no floor), Ceil: smallest element in array
BFloor: smallest element, Ceil: -1 (no ceil)
CFloor and Ceil both are -1
DFloor and Ceil both are the smallest element
Attempts:
2 left
💡 Hint
Floor must be less than or equal to the number; if none exists, floor is -1.
🧠 Conceptual
advanced
1:30remaining
Floor and ceil when number is greater than all elements
If the number is greater than all elements in a sorted array, what will be the floor and ceil?
AFloor: largest element in array, Ceil: -1 (no ceil)
BFloor: -1 (no floor), Ceil: largest element
CFloor and Ceil both are largest element
DFloor and Ceil both are -1
Attempts:
2 left
💡 Hint
Ceil must be greater than or equal to the number; if none exists, ceil is -1.
🔧 Debug
expert
2:30remaining
Identify the error in floor and ceil calculation code
What error does the following Go code produce when finding floor and ceil? arr := []int{2, 4, 6, 8} num := 5 floor, ceil := -1, -1 for _, v := range arr { if v < num { floor = v } if v > num && ceil == -1 { ceil = v } } fmt.Printf("Floor: %d, Ceil: %d", floor, ceil)
DSA Go
arr := []int{2, 4, 6, 8}
num := 5
floor, ceil := -1, -1
for _, v := range arr {
    if v < num {
        floor = v
    }
    if v > num && ceil == -1 {
        ceil = v
    }
}
fmt.Printf("Floor: %d, Ceil: %d", floor, ceil)
AFloor: -1, Ceil: 6
BFloor: 5, Ceil: 6
CFloor: 4, Ceil: -1
DFloor: 4, Ceil: 6
Attempts:
2 left
💡 Hint
Check the comparison operators used for floor and ceil conditions.