0
0
DSA Goprogramming~20 mins

Floor and Ceil in BST in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Floor and Ceil Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Floor and Ceil for a given key in BST
What is the output of the floor and ceil values for key 15 in the given BST?
DSA Go
package main

import "fmt"

type Node struct {
    val   int
    left  *Node
    right *Node
}

func floor(root *Node, key int) int {
    res := -1
    for root != nil {
        if root.val == key {
            return root.val
        } else if root.val > key {
            root = root.left
        } else {
            res = root.val
            root = root.right
        }
    }
    return res
}

func ceil(root *Node, key int) int {
    res := -1
    for root != nil {
        if root.val == key {
            return root.val
        } else if root.val < key {
            root = root.right
        } else {
            res = root.val
            root = root.left
        }
    }
    return res
}

func main() {
    root := &Node{20, nil, nil}
    root.left = &Node{10, nil, nil}
    root.right = &Node{30, nil, nil}
    root.left.left = &Node{5, nil, nil}
    root.left.right = &Node{15, nil, nil}
    root.right.left = &Node{25, nil, nil}
    root.right.right = &Node{35, nil, nil}

    f := floor(root, 15)
    c := ceil(root, 15)
    fmt.Printf("Floor: %d, Ceil: %d\n", f, c)
}
AFloor: 15, Ceil: 20
BFloor: 10, Ceil: 20
CFloor: 5, Ceil: 15
DFloor: 15, Ceil: 15
Attempts:
2 left
💡 Hint
Recall that floor is the greatest value less than or equal to the key, and ceil is the smallest value greater than or equal to the key.
Predict Output
intermediate
2:00remaining
Floor and Ceil for a key not present in BST
What is the output of floor and ceil for key 17 in the BST?
DSA Go
package main

import "fmt"

type Node struct {
    val   int
    left  *Node
    right *Node
}

func floor(root *Node, key int) int {
    res := -1
    for root != nil {
        if root.val == key {
            return root.val
        } else if root.val > key {
            root = root.left
        } else {
            res = root.val
            root = root.right
        }
    }
    return res
}

func ceil(root *Node, key int) int {
    res := -1
    for root != nil {
        if root.val == key {
            return root.val
        } else if root.val < key {
            root = root.right
        } else {
            res = root.val
            root = root.left
        }
    }
    return res
}

func main() {
    root := &Node{20, nil, nil}
    root.left = &Node{10, nil, nil}
    root.right = &Node{30, nil, nil}
    root.left.left = &Node{5, nil, nil}
    root.left.right = &Node{15, nil, nil}
    root.right.left = &Node{25, nil, nil}
    root.right.right = &Node{35, nil, nil}

    f := floor(root, 17)
    c := ceil(root, 17)
    fmt.Printf("Floor: %d, Ceil: %d\n", f, c)
}
AFloor: 10, Ceil: 25
BFloor: 15, Ceil: 20
CFloor: 17, Ceil: 17
DFloor: 20, Ceil: 25
Attempts:
2 left
💡 Hint
Floor is the greatest value less than or equal to 17, ceil is the smallest value greater than or equal to 17.
🔧 Debug
advanced
2:00remaining
Identify the error in floor function implementation
What error will occur when running the floor function below for key 12?
DSA Go
func floor(root *Node, key int) int {
    res := -1
    for root != nil {
        if root.val == key {
            return root.val
        } else if root.val < key {
            root = root.right
        } else {
            res = root.val
            root = root.left
        }
    }
    return res
}
ANil pointer dereference runtime error
BInfinite loop
CReturns incorrect floor value
DCompilation error
Attempts:
2 left
💡 Hint
Check the conditions for moving left or right in the BST for floor calculation.
Predict Output
advanced
2:00remaining
Floor and Ceil when key is smaller than all nodes
What is the output of floor and ceil for key 1 in the BST?
DSA Go
package main

import "fmt"

type Node struct {
    val   int
    left  *Node
    right *Node
}

func floor(root *Node, key int) int {
    res := -1
    for root != nil {
        if root.val == key {
            return root.val
        } else if root.val > key {
            root = root.left
        } else {
            res = root.val
            root = root.right
        }
    }
    return res
}

func ceil(root *Node, key int) int {
    res := -1
    for root != nil {
        if root.val == key {
            return root.val
        } else if root.val < key {
            root = root.right
        } else {
            res = root.val
            root = root.left
        }
    }
    return res
}

func main() {
    root := &Node{20, nil, nil}
    root.left = &Node{10, nil, nil}
    root.right = &Node{30, nil, nil}
    root.left.left = &Node{5, nil, nil}
    root.left.right = &Node{15, nil, nil}
    root.right.left = &Node{25, nil, nil}
    root.right.right = &Node{35, nil, nil}

    f := floor(root, 1)
    c := ceil(root, 1)
    fmt.Printf("Floor: %d, Ceil: %d\n", f, c)
}
AFloor: -1, Ceil: 5
BFloor: 5, Ceil: 10
CFloor: -1, Ceil: -1
DFloor: 1, Ceil: 5
Attempts:
2 left
💡 Hint
If key is smaller than all nodes, floor should be -1 indicating no floor.
🧠 Conceptual
expert
3:00remaining
Understanding floor and ceil behavior in BST with duplicates
In a BST that allows duplicate values inserted to the right subtree, what will be the floor and ceil of key 20 if the BST contains nodes with values [20, 20, 25, 15]?
AFloor: 20, Ceil: 20
BFloor: 15, Ceil: 25
CFloor: 20, Ceil: 25
DFloor: 15, Ceil: 20
Attempts:
2 left
💡 Hint
Even with duplicates inserted to the right subtree, since 20 exists, both floor and ceil are 20.