0
0
DSA Goprogramming~10 mins

BST vs Hash Map Trade-offs for Ordered Data in DSA Go - Interactive Comparison Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A<
B>
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes wrong tree structure.
2fill in blank
medium

Complete 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'
A[
B(
C{
D<-
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses causes syntax errors.
3fill in blank
hard

Fix 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'
A>
B!=
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' causes search to go wrong subtree.
4fill in blank
hard

Fill 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'
A>
Bv
Ck
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' includes wrong keys.
Assigning key instead of value.
5fill in blank
hard

Fill 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'
A>
Broot.key
Cresult
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' includes wrong keys.
Appending wrong variable.
Not passing result pointer in recursion.