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, which is inefficient compared to BSTs that can quickly navigate ranges.
Click to reveal answer
beginner
What is the average time complexity for search operations in a balanced BST and in a Hash Map?
Balanced BST: O(log n) due to tree height; Hash Map: O(1) average due to direct indexing via hash functions.
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 hash buckets and handling collisions, while BSTs use memory proportional to nodes with pointers only.
Click to reveal answer
advanced
What is a major trade-off when choosing a BST over a Hash Map for data storage?
BSTs provide ordered data and efficient range queries but have slower average search times (O(log n)) compared to Hash Maps' O(1) average search time, which lack order.
Click to reveal answer
Which data structure maintains elements in sorted order by default?
ABinary Search Tree
BHash Map
CStack
DQueue
✗ Incorrect
Binary Search Trees keep elements sorted, enabling ordered traversal.
What is the average search time complexity of a Hash Map?
AO(n)
BO(log n)
CO(1)
DO(n log n)
✗ Incorrect
Hash Maps provide average O(1) search time using hashing.
Which data structure is better for efficient range queries?
AHash Map
BLinked List
CArray
DBinary Search Tree
✗ Incorrect
BSTs allow efficient range queries due to their sorted structure.
What is a downside of using a Hash Map compared to a BST?
ANo ordering of keys
BUses less memory
CSlower average search time
DSupports range queries efficiently
✗ Incorrect
Hash Maps do not maintain any order of keys.
Which data structure typically uses more memory due to buckets and collision handling?
ABinary Search Tree
BHash Map
CArray
DStack
✗ 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, search speed, and memory.
You got /6 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 when you need to maintain elements in sorted order and perform fast range queries?
easy
A. Hash Map
B. Binary Search Tree (BST)
C. Stack
D. Queue
Solution
Step 1: Understand the need for sorted order
Maintaining elements in sorted order requires a data structure that keeps elements arranged by key values.
Step 2: Identify data structure supporting range queries
BST supports in-order traversal which gives sorted order and allows efficient range queries. Hash Map does not maintain order.
Final Answer:
Binary Search Tree (BST) -> Option B
Quick Check:
Sorted order + range queries = BST [OK]
Hint: Sorted order needs BST, hash maps don't keep order [OK]
Common Mistakes:
Thinking hash maps keep elements sorted
Confusing stack or queue with ordered data structures
2. Which of the following is the correct way to declare a hash map in C++ that maps integers to strings?
easy
A. unordered_map myMap;
B. std::map myMap;
C. std::unordered_map myMap;
D. hash_map myMap;
Solution
Step 1: Identify correct C++ hash map syntax
The standard C++ hash map is declared using std::unordered_map with template parameters for key and value types.
Step 2: Check namespace and types
std::unordered_map is correct. std::map is ordered map, not hash map. "hash_map" is non-standard. "unordered_map" without std:: is incomplete.
Final Answer:
std::unordered_map myMap; -> Option C
Quick Check:
Correct hash map syntax = std::unordered_map [OK]
Hint: Use std::unordered_map for hash map [OK]
Common Mistakes:
Using std::map instead of unordered_map for hash map
Omitting std:: namespace
Using non-standard hash_map
3. Consider the following C++ code using a BST and a hash map:
std::map is a BST-based ordered map, iterates keys in ascending order: 1 2 3.
Step 2: Understand std::unordered_map iteration order
std::unordered_map does not guarantee order; insertion order is not preserved. Output could be 3 1 2 or any order, but here likely 3 1 2 as inserted.
Final Answer:
1 2 3
3 1 2 -> Option D
Quick Check:
Ordered map prints sorted keys, hash map prints unordered keys [OK]
Hint: std::map prints sorted keys; unordered_map order is unpredictable [OK]
Common Mistakes:
Assuming unordered_map prints keys sorted
Confusing std::map and std::unordered_map iteration order
4. The following code attempts to use a BST and a hash map to store and print keys. Identify the error:
std::map bst;
bst[5] = "five";
bst[3] = "three";
std::unordered_map hashMap;
hashMap[5] = "five";
hashMap[3] = "three";
for (auto it = bst.begin(); it != bst.end(); ++it)
std::cout << it->second << " ";
for (auto it = hashMap.begin(); it != hashMap.end(); ++it)
std::cout << it->first << " ";
medium
A. Missing #include <unordered_map>
B. No error; code runs correctly
C. Using it->second in hashMap loop instead of it->first
D. Cannot use iterators with std::unordered_map
Solution
Step 1: Check code for missing includes
std::unordered_map requires including <unordered_map> header. Without it, code will not compile.
Step 2: Verify iterator usage and member access
Iterators and it->first/second usage are correct for both containers.
Final Answer:
Missing #include <unordered_map> -> Option A
Quick Check:
unordered_map needs #include <unordered_map> [OK]
Hint: Always include <unordered_map> for std::unordered_map [OK]
Common Mistakes:
Assuming iterators can't be used with unordered_map
Confusing it->first and it->second usage
Ignoring missing header includes
5. You need to store a large dataset of user IDs (integers) with their scores (integers). You want to quickly find the top 10 users with the highest scores and also check if a user ID exists efficiently. Which data structure or combination is best suited?
hard
A. Use a BST keyed by score and a hash map for user ID lookup
B. Use only a hash map for all operations
C. Use a hash map for user ID to score and a max-heap for top scores
D. Use a BST keyed by user ID only
Solution
Step 1: Identify requirements
Need fast user ID existence check and fast retrieval of top 10 users by score.
Step 2: Analyze data structure trade-offs
Hash map gives O(1) user ID lookup but no order. BST keyed by score allows ordered traversal for top scores. Combining BST keyed by score and hash map keyed by user ID allows both operations efficiently.
Final Answer:
Use a BST keyed by score and a hash map for user ID lookup -> Option A
Quick Check:
Top scores + fast ID check = BST + hash map combo [OK]
Hint: Combine BST (score) + hash map (ID) for order and fast lookup [OK]
Common Mistakes:
Using only hash map loses order for top scores
Using only BST keyed by ID can't get top scores easily
Ignoring need for two data structures
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.