0
0
DSA C++programming~20 mins

BST vs Hash Map Trade-offs for Ordered Data in DSA C++ - Compare & Choose

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ordered Data Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1: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?
AHash Maps automatically keep data sorted without extra effort.
BBSTs keep elements sorted, allowing in-order traversal to get data in ascending order.
CHash Maps use less memory than BSTs for storing ordered data.
DBSTs have faster average lookup time than Hash Maps.
Attempts:
2 left
💡 Hint
Think about how each data structure organizes data internally.
Predict Output
intermediate
2: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
}
ABST in-order: [3, 5, 7], Hash Map keys: [5, 3, 7] (order may vary)
BBST in-order: [5, 3, 7], Hash Map keys: [3, 5, 7]
CBST in-order: [7, 5, 3], Hash Map keys: [7, 5, 3]
DBST in-order: [3, 5, 7], Hash Map keys: [3, 5, 7]
Attempts:
2 left
💡 Hint
Remember that Hash Map iteration order is not guaranteed.
🔧 Debug
advanced
2: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);
}
ABecause the insert function uses recursion incorrectly causing infinite loops.
BBecause the insert function does not check for duplicates.
CBecause the root pointer is not updated after insertion.
DBecause inserting sorted data always adds nodes to the right, making the tree skewed.
Attempts:
2 left
💡 Hint
Think about how inserting sorted values affects BST shape.
🚀 Application
advanced
2: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?
ABalanced BST (like AVL or Red-Black Tree) with augmented node counts for range queries.
BUnordered Hash Map storing counts of each number.
CSimple array storing numbers unsorted.
DStack data structure storing numbers as they arrive.
Attempts:
2 left
💡 Hint
Think about how to efficiently get counts of numbers in a range.
Predict Output
expert
2: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;
}
A620
B580
C600
DSyntax error
Attempts:
2 left
💡 Hint
Add the values from the map in the order of BST in-order traversal.