Challenge - 5 Problems
Ordered Data Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Why choose a BST over a Hash Map for ordered data?
Which of the following is the main reason to prefer a Binary Search Tree (BST) over a Hash Map when you need to maintain ordered data?
Attempts:
2 left
💡 Hint
Think about how each data structure organizes data internally.
✗ Incorrect
BSTs store data in a way that the left child is smaller and the right child is larger, so an in-order traversal visits elements in sorted order. Hash Maps do not maintain any order.
❓ Predict Output
intermediate2:00remaining
Output of in-order traversal vs Hash Map iteration
Given the following BST insertion and Hash Map insertion, what will be the output of an in-order traversal of the BST and iteration over the Hash Map?
DSA C++
struct Node {
int val;
Node* left;
Node* right;
Node(int v) : val(v), left(nullptr), right(nullptr) {}
};
void insert(Node*& root, int v) {
if (!root) root = new Node(v);
else if (v < root->val) insert(root->left, v);
else insert(root->right, v);
}
void inorder(Node* root, std::vector<int>& res) {
if (!root) return;
inorder(root->left, res);
res.push_back(root->val);
inorder(root->right, res);
}
int main() {
Node* root = nullptr;
insert(root, 5);
insert(root, 3);
insert(root, 7);
std::unordered_map<int, int> map;
map[5] = 50;
map[3] = 30;
map[7] = 70;
std::vector<int> bst_vals;
inorder(root, bst_vals);
std::vector<int> map_keys;
for (auto& p : map) map_keys.push_back(p.first);
// Print bst_vals and map_keys
}Attempts:
2 left
💡 Hint
Remember that Hash Map iteration order is not guaranteed.
✗ Incorrect
BST in-order traversal visits nodes in ascending order: 3, 5, 7. Hash Map iteration order is unpredictable and may differ each run.
🔧 Debug
advanced2:00remaining
Why does this BST insertion cause unbalanced tree?
Consider this code inserting sorted data into a BST. Why does it cause the tree to become unbalanced (like a linked list)?
DSA C++
void insert(Node*& root, int v) { if (!root) root = new Node(v); else if (v < root->val) insert(root->left, v); else insert(root->right, v); } int main() { Node* root = nullptr; insert(root, 1); insert(root, 2); insert(root, 3); insert(root, 4); insert(root, 5); }
Attempts:
2 left
💡 Hint
Think about how inserting sorted values affects BST shape.
✗ Incorrect
Inserting sorted values into a BST without balancing causes all nodes to be added as right children, making the tree like a linked list.
🚀 Application
advanced2:00remaining
Choosing data structure for range queries on ordered data
You need to store a large set of numbers and frequently query how many numbers fall within a given range [low, high]. Which data structure is best suited for this task?
Attempts:
2 left
💡 Hint
Think about how to efficiently get counts of numbers in a range.
✗ Incorrect
Balanced BSTs can be augmented to store subtree sizes, allowing efficient range count queries. Hash Maps and arrays do not support efficient range queries.
❓ Predict Output
expert2:30remaining
Output of combined BST and Hash Map operations
What is the output of the following code snippet after all operations?
DSA C++
#include <iostream> #include <unordered_map> #include <vector> struct Node { int val; Node* left; Node* right; Node(int v) : val(v), left(nullptr), right(nullptr) {} }; void insert(Node*& root, int v) { if (!root) root = new Node(v); else if (v < root->val) insert(root->left, v); else insert(root->right, v); } void inorder(Node* root, std::vector<int>& res) { if (!root) return; inorder(root->left, res); res.push_back(root->val); inorder(root->right, res); } int main() { Node* root = nullptr; insert(root, 10); insert(root, 5); insert(root, 15); insert(root, 12); insert(root, 18); std::unordered_map<int, int> map; map[10] = 100; map[5] = 50; map[15] = 150; map[12] = 120; map[18] = 180; std::vector<int> bst_vals; inorder(root, bst_vals); int sum = 0; for (int v : bst_vals) { sum += map[v]; } std::cout << sum << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Add the values from the map in the order of BST in-order traversal.
✗ Incorrect
The BST in-order traversal visits nodes in ascending order: 5, 10, 12, 15, 18. Summing their mapped values: 50 + 100 + 120 + 150 + 180 = 600.