0
0
DSA C++programming~10 mins

BST vs Hash Map Trade-offs for Ordered Data in DSA C++ - 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 C++
if (root == nullptr) {
    root = new Node([1]);
}
Drag options to blanks, or click blank then click option'
Akey
Bvalue
Cnullptr
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' instead of 'key' when creating the node.
Assigning 'nullptr' instead of the key.
2fill in blank
medium

Complete the code to check if a key exists in a hash map.

DSA C++
if (hashMap.find([1]) != hashMap.end()) {
    // key exists
}
Drag options to blanks, or click blank then click option'
Akey
Bnullptr
ChashMap
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Searching by value instead of key.
Comparing with nullptr instead of end().
3fill in blank
hard

Fix the error in the BST in-order traversal code to print keys in ascending order.

DSA C++
void inorder(Node* root) {
    if (root == nullptr) return;
    inorder(root->[1]);
    std::cout << root->key << " ";
    inorder(root->right);
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Cparent
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Visiting right child before left child.
Using 'parent' pointer which may not exist.
4fill in blank
hard

Fill both blanks to create a hash map that stores keys and their counts only if count is greater than 1.

DSA C++
std::unordered_map<int, int> counts;
for (int num : nums) {
    counts[num]++;
}
auto filtered = [1];
for (auto& pair : counts) {
    if (pair.second [2] 1) {
        filtered[pair.first] = pair.second;
    }
}
Drag options to blanks, or click blank then click option'
Astd::unordered_map<int, int>()
B>
C==
Dstd::map<int, int>()
Attempts:
3 left
💡 Hint
Common Mistakes
Using std::map instead of unordered_map for filtered.
Using '==' instead of '>' in the condition.
5fill in blank
hard

Fill all three blanks to create a BST node insertion function that inserts keys smaller to the left and larger to the right.

DSA C++
Node* insert(Node* root, int key) {
    if (root == nullptr) {
        return new Node([1]);
    }
    if (key [2] root->key) {
        root->left = insert(root->left, key);
    } else if (key [3] root->key) {
        root->right = insert(root->right, key);
    }
    return root;
}
Drag options to blanks, or click blank then click option'
Akey
B<
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' in place of '<' or '>' in comparisons.
Returning nullptr instead of new node.