Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the current node is nil in the BST search function.
DSA Go
if root [1] nil { return false }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes wrong logic.
Using comparison operators like '<' or '>' with nil causes errors.
✗ Incorrect
We check if the current node is nil to know if the search reached a leaf without finding the value.
2fill in blank
mediumComplete the code to compare the search value with the current node's data.
DSA Go
if root.data [1] value { return true }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes incorrect search results.
Using '<' or '>' here is incorrect for equality check.
✗ Incorrect
If the current node's data equals the search value, we found the value in the BST.
3fill in blank
hardFix the error in the recursive call to search the left subtree when value is less than current node's data.
DSA Go
if value [1] root.data { return searchBST(root.left, value) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' causes searching the wrong subtree.
Using '==' or '!=' here is logically incorrect.
✗ Incorrect
If the value is less than the current node's data, we search the left subtree.
4fill in blank
hardFill both blanks to complete the recursive search for the right subtree when value is greater than current node's data.
DSA Go
if value [1] root.data { return searchBST(root.[2], value) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes wrong subtree search.
Using 'left' instead of 'right' causes wrong subtree search.
✗ Incorrect
If value is greater than current node's data, search the right subtree.
5fill in blank
hardFill all three blanks to complete the full BST search function in Go.
DSA Go
func searchBST(root *Node, value int) bool {
if root [1] nil {
return false
}
if root.data [2] value {
return true
}
if value [3] root.data {
return searchBST(root.left, value)
} else {
return searchBST(root.right, value)
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' for nil check.
Using '!=' instead of '==' for data equality.
Using '>' instead of '<' for left subtree search.
✗ Incorrect
The function checks if root is nil, then if root.data equals value, then searches left if value is less, else right.