Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

BST vs Hash Map Trade-offs for Ordered Data in DSA Javascript - Interactive Comparison Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a value into a Binary Search Tree (BST).

DSA Javascript
function insertNode(root, value) {
  if (root === null) {
    return { val: value, left: null, right: null };
  }
  if (value [1] root.val) {
    root.left = insertNode(root.left, value);
  } else {
    root.right = insertNode(root.right, value);
  }
  return root;
}
Drag options to blanks, or click blank then click option'
A<
B>
C===
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes wrong placement in the tree.
Using '===' will never insert new nodes properly.
2fill in blank
medium

Complete the code to check if a key exists in a JavaScript Map (hash map).

DSA Javascript
function hasKey(map, key) {
  return map.[1](key);
}
Drag options to blanks, or click blank then click option'
Ahas
Bfind
Ccontains
Dincludes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'includes' which works on arrays, not Maps.
Using 'find' which is not a Map method.
3fill in blank
hard

Fix the error in the code to find the minimum value node in a BST.

DSA Javascript
function findMinNode(node) {
  while (node.[1] !== null) {
    node = node.left;
  }
  return node;
}
Drag options to blanks, or click blank then click option'
Aright
Bval
Cparent
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Checking 'right' instead of 'left' leads to wrong minimum.
Checking 'val' or 'parent' properties causes errors.
4fill in blank
hard

Fill both blanks to create a Map from an array of keys with initial value 0, but only for keys longer than 3 characters.

DSA Javascript
const keys = ['apple', 'bat', 'carrot', 'dog'];
const map = new Map();
for (const key of keys) {
  if (key.[1] > 3) {
    map.[2](key, 0);
  }
}
Drag options to blanks, or click blank then click option'
Alength
Bset
Cpush
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' instead of 'length' for strings.
Using 'push' which is for arrays, not Maps.
5fill in blank
hard

Fill all three blanks to create an object that maps keys to their lengths, but only include keys with length less than 5.

DSA Javascript
const keys = ['sun', 'moon', 'star', 'sky'];
const result = {
  [1]: [2] for [3] of keys if [2] < 5
};
Drag options to blanks, or click blank then click option'
Akey
Blen
Cfor key
Dkey.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len' which is Python style, not JavaScript.
Confusing the loop syntax.

Practice

(1/5)
1. Which data structure is best suited when you need to maintain elements in a sorted order and perform range queries efficiently?
easy
A. Binary Search Tree (BST)
B. Hash Map
C. Stack
D. Queue

Solution

  1. Step 1: Understand the need for sorted order

    Maintaining elements in sorted order requires a structure that keeps data sorted as elements are added or removed.
  2. Step 2: Compare BST and Hash Map for sorted data

    BST keeps elements sorted and supports efficient range queries, while Hash Map does not maintain order.
  3. Final Answer:

    Binary Search Tree (BST) -> Option A
  4. Quick Check:

    Sorted data + range queries = BST [OK]
Hint: Sorted order needs BST, hash maps don't keep order [OK]
Common Mistakes:
  • Thinking hash maps keep data sorted
  • Confusing stack or queue with sorted structures
  • Assuming hash maps support range queries
2. Which of the following is the correct way to declare a Hash Map (Map) in JavaScript?
easy
A. const map = {};
B. const map = Map();
C. const map = new HashMap();
D. const map = new Map();

Solution

  1. Step 1: Recall JavaScript Map syntax

    JavaScript uses the Map object with the syntax new Map() to create a hash map.
  2. Step 2: Check each option

    const map = new Map(); uses new Map(), which is correct. const map = Map(); misses 'new'. const map = {}; creates a plain object, not a Map. const map = new HashMap(); uses a non-existent HashMap class.
  3. Final Answer:

    const map = new Map(); -> Option D
  4. Quick Check:

    Map creation syntax = new Map() [OK]
Hint: Use new Map() to create a hash map in JavaScript [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Using plain objects instead of Map
  • Assuming HashMap class exists in JS
3. Consider the following JavaScript code using a BST class with insert and inorderTraversal methods:
const bst = new BST();
bst.insert(10);
bst.insert(5);
bst.insert(15);
console.log(bst.inorderTraversal());

What will be the output?
medium
A. [10, 5, 15]
B. [5, 10, 15]
C. [15, 10, 5]
D. [5, 15, 10]

Solution

  1. Step 1: Understand inorder traversal of BST

    Inorder traversal visits nodes in ascending order: left subtree, root, right subtree.
  2. Step 2: Trace insertion and traversal

    Insert 10 as root, 5 to left, 15 to right. Inorder traversal visits 5, then 10, then 15.
  3. Final Answer:

    [5, 10, 15] -> Option B
  4. Quick Check:

    Inorder traversal = sorted nodes [OK]
Hint: Inorder traversal of BST returns sorted array [OK]
Common Mistakes:
  • Assuming insertion order equals traversal order
  • Confusing preorder or postorder with inorder
  • Expecting hash map order instead of BST order
4. The following code attempts to use a Hash Map to maintain sorted keys. What is the main issue?
const map = new Map();
map.set(3, 'c');
map.set(1, 'a');
map.set(2, 'b');
console.log([...map.keys()]);
medium
A. Keys are not guaranteed to be in sorted order
B. Map keys must be strings only
C. Map cannot store numbers as keys
D. map.set() syntax is incorrect

Solution

  1. Step 1: Understand Map key order behavior

    JavaScript Map preserves insertion order, not sorted order of keys.
  2. Step 2: Analyze the output of keys()

    Keys will be in insertion order [3,1,2], not sorted [1,2,3].
  3. Final Answer:

    Keys are not guaranteed to be in sorted order -> Option A
  4. Quick Check:

    Map keys order = insertion order, not sorted [OK]
Hint: Map keys keep insertion order, not sorted order [OK]
Common Mistakes:
  • Thinking Map sorts keys automatically
  • Believing Map keys must be strings
  • Confusing Map with Object key behavior
5. You need to store a large dataset with frequent insertions and lookups by key, but also require fast retrieval of all keys in sorted order. Which approach is best?
hard
A. Use an Array and perform linear search
B. Use a Hash Map alone and sort keys on demand
C. Use a Balanced BST to store keys and values
D. Use a Queue to maintain insertion order

Solution

  1. Step 1: Analyze requirements for insertions, lookups, and sorted retrieval

    Frequent insertions and lookups need efficient operations; sorted retrieval requires order maintenance.
  2. Step 2: Compare data structures for these needs

    Hash Map offers fast lookups but no order; sorting keys on demand is costly. Balanced BST maintains sorted order and supports efficient insertions and lookups.
  3. Final Answer:

    Use a Balanced BST to store keys and values -> Option C
  4. Quick Check:

    Balanced BST = fast insert, lookup + sorted keys [OK]
Hint: Balanced BST combines fast lookup and sorted order [OK]
Common Mistakes:
  • Choosing hash map without sorting cost consideration
  • Using array causing slow lookups
  • Using queue which doesn't support sorted retrieval
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard