Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a key into a Binary Search Tree (BST).
DSA Java
if (root == null) { root = new TreeNode([1]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' instead of 'key' when creating the node.
✗ Incorrect
The BST node is created with the key value when the root is null.
2fill in blank
mediumComplete the code to check if a key exists in a HashMap.
DSA Java
if (map.[1](key)) { return true; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using containsValue instead of containsKey.
✗ Incorrect
HashMap's containsKey method checks if the key exists.
3fill in blank
hardFix the error in the BST search code to compare keys correctly.
DSA Java
if (key.[1](root.key) < 0) { return search(root.left, key); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using equals instead of compareTo for ordering.
✗ Incorrect
Use compareTo to compare keys in BST for ordering.
4fill in blank
hardFill both blanks to create a TreeMap and insert a key-value pair.
DSA Java
Map<String, Integer> map = new [1]<>(); map.[2]("apple", 5);
Attempts:
3 left
💡 Hint
Common Mistakes
Using HashMap instead of TreeMap for ordered data.
✗ Incorrect
TreeMap maintains order; put inserts key-value pairs.
5fill in blank
hardFill all three blanks to iterate over a TreeMap and print keys in order.
DSA Java
for ([1] key : map.[2]()) { System.out.println(key + ": " + map.[3](key)); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using entrySet() but iterating as keys only.
✗ Incorrect
Use String for keys, keySet() to get keys in order, and get() to access values.
