0
0
DSA Typescriptprogramming~10 mins

BST vs Hash Map Trade-offs for Ordered Data in DSA Typescript - 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 Typescript
function insertBST(root: TreeNode | null, val: number): TreeNode {
  if (root === null) {
    return new TreeNode(val);
  }
  if (val [1] root.val) {
    root.left = insertBST(root.left, val);
  } else {
    root.right = insertBST(root.right, val);
  }
  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 BST.
Using '===' will never insert properly.
2fill in blank
medium

Complete the code to check if a key exists in a Hash Map.

DSA Typescript
function hasKey(map: Map<string, number>, key: string): boolean {
  return map.[1](key);
}
Drag options to blanks, or click blank then click option'
Aget
Bcontains
Cfind
Dhas
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' returns the value or undefined, not a boolean.
Using 'find' or 'contains' are not valid Map methods.
3fill in blank
hard

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

DSA Typescript
function findMin(root: TreeNode | null): number | null {
  if (root === null) return null;
  let current = root;
  while (current.[1] !== null) {
    current = current.left;
  }
  return current.val;
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' will find the maximum, not minimum.
Using 'parent' or 'child' are invalid properties.
4fill in blank
hard

Fill both blanks to create a function that returns keys in sorted order from a BST.

DSA Typescript
function inorderTraversal(root: TreeNode | null, result: number[] = []): number[] {
  if (root !== null) {
    inorderTraversal(root.[1], result);
    result.push(root.val);
    inorderTraversal(root.[2], result);
  }
  return result;
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right changes the order.
Using 'parent' or 'child' causes errors.
5fill in blank
hard

Fill all three blanks to create a function that counts keys in a Hash Map greater than a threshold.

DSA Typescript
function countGreaterThan(map: Map<string, number>, threshold: number): number {
  let count = 0;
  for (const [1] of map.entries()) {
    const [key, value] = [2];
    if (value [3] threshold) {
      count++;
    }
  }
  return count;
}
Drag options to blanks, or click blank then click option'
Aentry
B>
C<
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' counts values less than threshold.
Using wrong variable names causes errors.