Challenge - 5 Problems
BST vs Hash Map Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Inorder Traversal on BST Insertions
Given the following sequence of insertions into an empty Binary Search Tree (BST), what is the output of an inorder traversal?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } class BST { Node root; void insert(int v) { root = insertRec(root, v); } Node insertRec(Node root, int v) { if (root == null) return new Node(v); if (v < root.val) root.left = insertRec(root.left, v); else root.right = insertRec(root.right, v); return root; } void inorder(Node root, StringBuilder sb) { if (root != null) { inorder(root.left, sb); sb.append(root.val).append(" -> "); inorder(root.right, sb); } } } BST tree = new BST(); tree.insert(50); tree.insert(30); tree.insert(70); tree.insert(20); tree.insert(40); tree.insert(60); tree.insert(80); StringBuilder sb = new StringBuilder(); tree.inorder(tree.root, sb); System.out.println(sb.toString() + "null");
Attempts:
2 left
💡 Hint
Remember that inorder traversal of a BST visits nodes in ascending order.
✗ Incorrect
Inorder traversal visits left subtree, then root, then right subtree. For BST, this results in sorted order of inserted values.
🧠 Conceptual
intermediateWhy Hash Maps Do Not Maintain Order
Which of the following best explains why a standard hash map does not maintain the order of inserted elements?
Attempts:
2 left
💡 Hint
Think about how hashing works and how keys are placed.
✗ Incorrect
Hash maps use a hash function to assign keys to buckets. This process does not preserve any order of insertion or key value.
❓ Predict Output
advancedOutput of Searching in BST vs Hash Map
Given a BST and a Hash Map both containing the same keys and values, what is the output of searching for key 25 in each data structure?
DSA Java
import java.util.HashMap; class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } class BST { Node root; void insert(int v) { root = insertRec(root, v); } Node insertRec(Node root, int v) { if (root == null) return new Node(v); if (v < root.val) root.left = insertRec(root.left, v); else root.right = insertRec(root.right, v); return root; } boolean search(Node root, int key) { if (root == null) return false; if (root.val == key) return true; if (key < root.val) return search(root.left, key); else return search(root.right, key); } } BST tree = new BST(); int[] keys = {10, 20, 30, 40, 50}; for (int k : keys) tree.insert(k); HashMap<Integer, String> map = new HashMap<>(); for (int k : keys) map.put(k, "Value" + k); boolean bstResult = tree.search(tree.root, 25); String mapResult = map.get(25); System.out.println("BST search: " + bstResult); System.out.println("HashMap search: " + mapResult);
Attempts:
2 left
💡 Hint
Check if key 25 was inserted in either data structure.
✗ Incorrect
Key 25 was not inserted, so BST search returns false and HashMap returns null for get(25).
🧠 Conceptual
advancedTrade-offs of Using BST vs Hash Map for Ordered Data
Which statement correctly describes a trade-off when choosing between a BST and a Hash Map for storing ordered data?
Attempts:
2 left
💡 Hint
Think about order maintenance and lookup speed differences.
✗ Incorrect
BST keeps keys sorted and supports range queries but lookup is O(log n) average; Hash Map lookup is O(1) average but does not keep order.
🚀 Application
expertChoosing Data Structure for Real-Time Sorted Data Updates
You need to design a system that frequently inserts and deletes integer keys and must always provide the current sorted list of keys efficiently. Which data structure is the best choice?
Attempts:
2 left
💡 Hint
Consider both update efficiency and maintaining sorted order.
✗ Incorrect
Self-balancing BST keeps data sorted and supports efficient insertions and deletions, unlike hash maps or unsorted lists.
