0
0
DSA Javascriptprogramming~10 mins

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

Choose your learning style9 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.