0
0
DSA Typescriptprogramming~20 mins

BST vs Hash Map Trade-offs for Ordered Data in DSA Typescript - 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
2:00remaining
Why choose a BST over a Hash Map for ordered data?

Which reason best explains why a Binary Search Tree (BST) is preferred over a Hash Map when you need to maintain data in order?

ABST keeps elements sorted, allowing in-order traversal to get data in ascending order.
BBST has faster average lookup time than Hash Map.
CHash Map automatically sorts keys, so BST is unnecessary.
DBST uses less memory than Hash Map in all cases.
Attempts:
2 left
💡 Hint

Think about how each data structure stores and accesses data.

Predict Output
intermediate
2:00remaining
Output of in-order traversal vs Hash Map iteration

Given the following data inserted in this order: 5, 3, 7 into a BST and a Hash Map, what is the output of an in-order traversal of the BST and iteration over the Hash Map?

DSA Typescript
const bst = new BST();
bst.insert(5);
bst.insert(3);
bst.insert(7);

const hashMap = new Map();
hashMap.set(5, 'five');
hashMap.set(3, 'three');
hashMap.set(7, 'seven');

// Output BST in-order traversal keys
// Output Hash Map keys iteration order
A
BST in-order: 7 -> 5 -> 3
Hash Map iteration: 7, 5, 3
B
BST in-order: 5 -> 3 -> 7
Hash Map iteration: 3, 5, 7
C
BST in-order: 3 -> 5 -> 7
Hash Map iteration: 5, 3, 7
D
BST in-order: 3 -> 7 -> 5
Hash Map iteration: 5, 7, 3
Attempts:
2 left
💡 Hint

Remember how in-order traversal works and how Map preserves insertion order.

🔧 Debug
advanced
2:30remaining
Why does this BST code fail to maintain order?

Consider this simplified BST insert code snippet. Why does it fail to keep the tree ordered?

DSA Typescript
class Node {
  constructor(public val: number, public left: Node | null = null, public right: Node | null = null) {}
}

class BST {
  root: Node | null = null;

  insert(val: number) {
    if (!this.root) {
      this.root = new Node(val);
      return;
    }
    let current = this.root;
    while (current) {
      if (val < current.val) {
        current.left = new Node(val);
        break;
      } else {
        current.right = new Node(val);
        break;
      }
    }
  }
}
AIt overwrites left or right child without checking if they are null, losing existing nodes.
BIt uses a while loop but never updates current, causing infinite loop.
CIt inserts duplicates without checking for equality.
DIt does not create new nodes for values greater than root.
Attempts:
2 left
💡 Hint

Look at how the left and right children are assigned inside the loop.

🚀 Application
advanced
2:00remaining
Choosing data structure for range queries on ordered data

You need to frequently find all keys between two values (range queries) in a large dataset. Which data structure is best suited?

AStack, because it stores elements in order of insertion.
BHash Map, because it has constant time lookup for all keys.
CArray, because sorting once is enough for all queries.
DBinary Search Tree, because it supports efficient in-order traversal for range queries.
Attempts:
2 left
💡 Hint

Think about which structure allows ordered traversal and efficient range filtering.

Predict Output
expert
3:00remaining
Output after mixed BST and Hash Map operations

Given the following operations, what is the final output of the BST in-order traversal and the Hash Map keys iteration?

DSA Typescript
const bst = new BST();
bst.insert(10);
bst.insert(5);
bst.insert(15);
bst.insert(12);
bst.insert(18);

const hashMap = new Map();
hashMap.set(10, 'ten');
hashMap.set(5, 'five');
hashMap.set(15, 'fifteen');
hashMap.delete(5);
hashMap.set(12, 'twelve');
hashMap.set(18, 'eighteen');

// Print BST in-order traversal keys
// Print Hash Map keys iteration order
A
BST in-order: 5 -&gt; 10 -&gt; 12 -&gt; 15 -&gt; 18
Hash Map iteration: 5, 10, 15, 12, 18
B
BST in-order: 5 -&gt; 10 -&gt; 12 -&gt; 15 -&gt; 18
Hash Map iteration: 10, 15, 12, 18
C
BST in-order: 10 -&gt; 5 -&gt; 12 -&gt; 15 -&gt; 18
Hash Map iteration: 5, 10, 15, 12, 18
D
BST in-order: 5 -&gt; 10 -&gt; 15 -&gt; 12 -&gt; 18
Hash Map iteration: 10, 5, 15, 12, 18
Attempts:
2 left
💡 Hint

Remember that deleting a key from Map removes it from iteration, and BST in-order traversal is sorted.