What is a key advantage of a Binary Search Tree (BST) over a Hash Map when working with ordered data?
A BST maintains elements in sorted order, allowing efficient in-order traversal to access data in ascending or descending order.
Click to reveal answer
intermediate
Why might a Hash Map be less suitable than a BST for range queries?
Hash Maps do not maintain any order of keys, so range queries require checking all keys, making them inefficient compared to BSTs which can quickly find ranges.
Click to reveal answer
beginner
What is the average time complexity for search operations in a balanced BST and a Hash Map?
Balanced BST: O(log n) average time; Hash Map: O(1) average time for search.
Click to reveal answer
intermediate
How does memory usage typically compare between BSTs and Hash Maps?
Hash Maps often use more memory due to storing keys, values, and handling collisions with extra space, while BSTs use memory mainly for nodes and pointers.
Click to reveal answer
intermediate
What is a trade-off when choosing a BST over a Hash Map for data storage?
BSTs provide ordered data access and efficient range queries but have slower average search times (O(log n)) compared to Hash Maps' O(1) search, which lack order.
Click to reveal answer
Which data structure maintains elements in sorted order by default?
AHash Map
BBinary Search Tree
CStack
DQueue
✗ Incorrect
Binary Search Trees keep elements sorted, enabling ordered traversal.
What is the average search time complexity of a Hash Map?
AO(1)
BO(log n)
CO(n)
DO(n log n)
✗ Incorrect
Hash Maps provide average O(1) time for search due to direct key hashing.
Which data structure is better for efficient range queries?
AArray
BHash Map
CBinary Search Tree
DLinked List
✗ Incorrect
BSTs maintain order, allowing efficient range queries by traversing relevant nodes.
What is a downside of using a Hash Map compared to a BST?
ASlower search time
BUses less memory
CSupports range queries efficiently
DNo order of keys
✗ Incorrect
Hash Maps do not keep keys in any order, making ordered operations difficult.
Which data structure typically uses more memory due to collision handling?
AHash Map
BQueue
CStack
DBinary Search Tree
✗ Incorrect
Hash Maps use extra memory for buckets and collision resolution.
Explain the main trade-offs between using a Binary Search Tree and a Hash Map for storing ordered data.
Think about order, speed, and memory.
You got /5 concepts.
Describe scenarios where a Binary Search Tree is preferred over a Hash Map.
Focus on order and query types.
You got /4 concepts.
Practice
(1/5)
1. Which data structure is best suited for maintaining data in sorted order with efficient range queries?
easy
A. Hash Map
B. Stack
C. Binary Search Tree (BST)
D. Queue
Solution
Step 1: Understand data ordering requirements
Range queries and sorted data require maintaining order among elements.
Step 2: Compare BST and Hash Map for ordering
BST keeps elements in sorted order by design; Hash Map does not maintain order.
Final Answer:
Binary Search Tree (BST) -> Option C
Quick Check:
Ordered data = BST [OK]
Hint: For sorted data and range queries, choose BST [OK]
Common Mistakes:
Thinking hash maps maintain order
Confusing stack or queue as ordered structures
Ignoring the need for range queries
2. Which of the following is the correct way to declare a hash map in C using standard libraries?
easy
A. #include \nhash_map map;
B. #include \nstruct hashmap map;
C. #include \nint map[100];
D. #include \nGHashTable *map = g_hash_table_new(g_int_hash, g_int_equal);
Solution
Step 1: Identify standard C hash map usage
C standard library does not have built-in hash maps; GLib provides GHashTable.
Step 2: Check options for valid syntax
#include <glib.h>\nGHashTable *map = g_hash_table_new(g_int_hash, g_int_equal); uses GLib's GHashTable correctly; others are invalid or incomplete.
Final Answer:
#include <glib.h>\nGHashTable *map = g_hash_table_new(g_int_hash, g_int_equal); -> Option D
Hint: Use GLib for hash maps in C, not standard library [OK]
Common Mistakes:
Assuming C has built-in hash map
Using C++ syntax in C code
Confusing arrays with hash maps
3. Consider the following code snippet using a BST and a hash map for storing integer keys. What will be the output of the in-order traversal of the BST after inserting keys 5, 3, 7, 2, 4?
medium
A. 2 -> 3 -> 4 -> 5 -> 7 -> null
B. 5 -> 3 -> 7 -> 2 -> 4 -> null
C. 7 -> 5 -> 4 -> 3 -> 2 -> null
D. 3 -> 2 -> 4 -> 5 -> 7 -> null
Solution
Step 1: Insert keys into BST
Insert 5 as root, then 3 left of 5, 7 right of 5, 2 left of 3, 4 right of 3.
Step 2: Perform in-order traversal
In-order visits left subtree, root, right subtree: 2, 3, 4, 5, 7.
Final Answer:
2 -> 3 -> 4 -> 5 -> 7 -> null -> Option A
Quick Check:
In-order BST traversal = sorted keys [OK]
Hint: In-order traversal of BST gives sorted keys [OK]
Common Mistakes:
Confusing insertion order with traversal order
Mixing pre-order or post-order traversal
Assuming hash map order applies to BST
4. The following C code attempts to insert keys into a BST but causes a runtime error. What is the main issue?
A. newNode's left and right pointers are not initialized
B. malloc is not included via stdlib.h
C. The function does not handle duplicate keys
D. The root pointer is not updated in main
Solution
Step 1: Check new node initialization
newNode's left and right pointers are not set, so they contain garbage values.
Step 2: Understand impact on BST
Uninitialized pointers cause undefined behavior or runtime errors during traversal or further insertions.
Final Answer:
newNode's left and right pointers are not initialized -> Option A
Quick Check:
Initialize pointers to NULL to avoid errors [OK]
Hint: Always initialize new node pointers to NULL [OK]
Common Mistakes:
Ignoring pointer initialization
Assuming malloc auto-initializes memory
Confusing duplicate key handling with runtime error
5. You need to store a large dataset of user IDs with associated scores. You want to quickly find users with scores in a specific range and also retrieve scores by user ID efficiently. Which combination of data structures is best?
hard
A. Use only a hash map keyed by user ID
B. Use a hash map for user ID to score and a balanced BST keyed by score for range queries
C. Use only a BST keyed by user ID
D. Use an array sorted by user ID
Solution
Step 1: Identify requirements
Fast lookup by user ID and efficient range queries by score are needed.
Step 2: Match data structures to needs
Hash map gives O(1) user ID lookup; balanced BST keyed by score supports range queries efficiently.
Step 3: Combine for best performance
Using both structures together meets both requirements effectively.
Final Answer:
Use a hash map for user ID to score and a balanced BST keyed by score for range queries -> Option B
Quick Check:
Hash map + BST combo = best for lookup + range [OK]
Hint: Combine hash map for lookup and BST for range queries [OK]
Common Mistakes:
Using only one structure and missing efficiency
Using array which is slow for lookups
Ignoring need for range queries
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.