0
0
DSA Javascriptprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST vs Hash Map Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
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) instead of a Hash Map when you need to keep data in order?

ABST keeps data sorted and allows in-order traversal to get ordered data easily.
BBST uses less memory than Hash Map in all cases.
CBST has faster average lookup time than Hash Map.
DBST automatically handles duplicate keys better than Hash Map.
Attempts:
2 left
💡 Hint

Think about how you get sorted data from each structure.

Predict Output
intermediate
2:00remaining
Output of in-order traversal on BST

What is the output of the in-order traversal of this BST?

DSA Javascript
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function inorder(root, result = []) {
  if (!root) return result;
  inorder(root.left, result);
  result.push(root.val);
  inorder(root.right, result);
  return result;
}

// Construct BST
const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(3);
root.left.right = new Node(7);
root.right.right = new Node(18);

console.log(inorder(root));
A[18, 15, 10, 7, 5, 3]
B[10, 5, 3, 7, 15, 18]
C[3, 5, 7, 10, 15, 18]
D[5, 3, 7, 10, 15, 18]
Attempts:
2 left
💡 Hint

In-order traversal visits left subtree, node, then right subtree.

Predict Output
advanced
2:00remaining
Hash Map key order after insertions

What will be the order of keys when iterating over this JavaScript Map?

DSA Javascript
const map = new Map();
map.set('b', 2);
map.set('a', 1);
map.set('c', 3);

const keys = [];
for (const key of map.keys()) {
  keys.push(key);
}
console.log(keys);
A[]
B["b", "a", "c"]
C["c", "b", "a"]
D["a", "b", "c"]
Attempts:
2 left
💡 Hint

JavaScript Map preserves insertion order of keys.

🧠 Conceptual
advanced
2:00remaining
Time complexity trade-offs between BST and Hash Map

Which statement correctly compares average time complexities of search operations in a balanced BST and a Hash Map?

ABalanced BST search is O(log n), Hash Map search is O(1) on average.
BBalanced BST search is O(n), Hash Map search is O(log n) on average.
CBalanced BST search is O(1), Hash Map search is O(log n) on average.
DBoth Balanced BST and Hash Map have O(n) search time on average.
Attempts:
2 left
💡 Hint

Think about how balanced trees and hash maps organize data.

🚀 Application
expert
3:00remaining
Choosing data structure for range queries on ordered data

You need to store a large set of numbers and frequently perform range queries (e.g., find all numbers between 10 and 50). Which data structure is best suited for this task?

AStack, because it allows easy access to the last inserted number.
BHash Map, because it provides constant time lookups for any number.
CUnsorted Array, because it uses less memory and is faster for insertions.
DBalanced Binary Search Tree, because it supports efficient range queries via in-order traversal.
Attempts:
2 left
💡 Hint

Consider which structure allows ordered traversal and efficient range filtering.