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?
Think about how you get sorted data from each structure.
BST stores data in a way that allows in-order traversal to retrieve sorted data. Hash Maps do not maintain order.
What is the output of the in-order traversal of this BST?
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));
In-order traversal visits left subtree, node, then right subtree.
In-order traversal of BST visits nodes in ascending order.
What will be the order of keys when iterating over this JavaScript Map?
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);
JavaScript Map preserves insertion order of keys.
Map keys are iterated in the order they were added.
Which statement correctly compares average time complexities of search operations in a balanced BST and a Hash Map?
Think about how balanced trees and hash maps organize data.
Balanced BSTs have logarithmic search time; Hash Maps have constant average search time.
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?
Consider which structure allows ordered traversal and efficient range filtering.
Balanced BSTs keep data sorted and allow efficient range queries by traversing only relevant parts.