Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start searching for the floor value in the BST.
DSA Go
func floorInBST(root *Node, key int) *Node {
var floorNode *Node = nil
current := [1]
for current != nil {
if current.val == key {
return current
} else if current.val > key {
current = current.left
} else {
floorNode = current
current = current.right
}
}
return floorNode
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing current to nil causes no traversal.
Starting from root.left or root.right skips part of the tree.
✗ Incorrect
We start searching from the root node, so current should be initialized to root.
2fill in blank
mediumComplete the code to update the ceilNode when current node value is greater than the key.
DSA Go
func ceilInBST(root *Node, key int) *Node {
var ceilNode *Node = nil
current := root
for current != nil {
if current.val == key {
return current
} else if current.val > key {
ceilNode = [1]
current = current.left
} else {
current = current.right
}
}
return ceilNode
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning ceilNode to current.left or current.right instead of current.
Not updating ceilNode at all.
✗ Incorrect
When current.val is greater than key, current could be the ceil, so we update ceilNode to current.
3fill in blank
hardFix the error in the recursive floor function to correctly return the floor node.
DSA Go
func floorRecursive(root *Node, key int) *Node {
if root == nil {
return nil
}
if root.val == key {
return root
}
if root.val > key {
return [1]
}
floorRight := floorRecursive(root.right, key)
if floorRight != nil && floorRight.val <= key {
return floorRight
}
return root
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Recursing on right subtree when root.val > key.
Returning nil too early.
✗ Incorrect
If root.val > key, floor must be in left subtree, so recurse on root.left.
4fill in blank
hardFill both blanks to complete the iterative ceil function in BST.
DSA Go
func ceilInBSTIter(root *Node, key int) *Node {
var ceilNode *Node = nil
current := root
for current != nil {
if current.val == key {
return current
} else if current.val < key {
current = [1]
} else {
ceilNode = current
current = [2]
}
}
return ceilNode
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right moves.
Not updating ceilNode before moving left.
✗ Incorrect
If current.val < key, move right to find larger values; else update ceilNode and move left.
5fill in blank
hardFill all three blanks to complete the recursive ceil function in BST.
DSA Go
func ceilRecursive(root *Node, key int) *Node {
if root == nil {
return nil
}
if root.val == key {
return root
}
if root.val < key {
return [1]
}
ceilLeft := [2]
if ceilLeft != nil && ceilLeft.val >= key {
return ceilLeft
}
return [3]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Searching wrong subtree based on root.val and key.
Returning incorrect node when no ceil found in left subtree.
✗ Incorrect
If root.val < key, ceil is in right subtree; else check left subtree, else root is ceil.