Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a key into a binary search tree (BST).
DSA Go
func insert(root *Node, key int) *Node {
if root == nil {
return &Node{key: key}
}
if key [1] root.key {
root.left = insert(root.left, key)
} else {
root.right = insert(root.right, key)
}
return root
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes wrong tree structure.
✗ Incorrect
In a BST, keys less than the current node go to the left subtree.
2fill in blank
mediumComplete the code to check if a key exists in a hash map.
DSA Go
func containsKey(m map[int]string, key int) bool {
_, ok := m[1]key
return ok
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses causes syntax errors.
✗ Incorrect
In Go, map keys are accessed using square brackets.
3fill in blank
hardFix the error in the BST search function to correctly find a key.
DSA Go
func search(root *Node, key int) bool {
if root == nil {
return false
}
if key == root.key {
return true
} else if key [1] root.key {
return search(root.left, key)
} else {
return search(root.right, key)
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' causes search to go wrong subtree.
✗ Incorrect
To search in BST, go left if key is less than current node's key.
4fill in blank
hardFill both blanks to create a map from keys to their values only if the key is greater than 10.
DSA Go
filtered := map[int]string{}
for k, v := range data {
if k [1] 10 {
filtered[k] = [2]
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' includes wrong keys.
Assigning key instead of value.
✗ Incorrect
We check if key is greater than 10, then assign the value to filtered map.
5fill in blank
hardFill all three blanks to create a BST in-order traversal that collects keys greater than 5.
DSA Go
func inorder(root *Node, result *[]int) {
if root == nil {
return
}
inorder(root.left, result)
if root.key [1] 5 {
*result = append(*result, [2])
}
inorder(root.right, [3])
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' includes wrong keys.
Appending wrong variable.
Not passing result pointer in recursion.
✗ Incorrect
Check if key is greater than 5, append key to result slice, and continue traversal.