Challenge - 5 Problems
Floor and Ceil Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
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.
✗ Incorrect
Since 7 is present in the array, both floor and ceil are 7.
❓ Predict Output
intermediate2: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)
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.
✗ Incorrect
6 is not in the array. The greatest element less than 6 is 5 (floor), and the smallest element greater than 6 is 7 (ceil).
🧠 Conceptual
advanced1: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?
Attempts:
2 left
💡 Hint
Floor must be less than or equal to the number; if none exists, floor is -1.
✗ Incorrect
If the number is smaller than all elements, no floor exists (-1), but ceil is the smallest element.
🧠 Conceptual
advanced1: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?
Attempts:
2 left
💡 Hint
Ceil must be greater than or equal to the number; if none exists, ceil is -1.
✗ Incorrect
If the number is greater than all elements, floor is the largest element, but no ceil exists (-1).
🔧 Debug
expert2: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)
Attempts:
2 left
💡 Hint
Check the comparison operators used for floor and ceil conditions.
✗ Incorrect
The code uses '<' and '>' which exclude equality. Since 5 is not in the array, floor is the greatest element less than 5 (4), and ceil is the smallest element greater than 5 (6).