Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes wrong placement in BST.
Using '===' will never insert properly.
✗ Incorrect
In a BST, values less than the current node go to the left subtree.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The has method checks if a key exists in a Map.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' will find the maximum, not minimum.
Using 'parent' or 'child' are invalid properties.
✗ Incorrect
The minimum value in a BST is found by going left until no more left child exists.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right changes the order.
Using 'parent' or 'child' causes errors.
✗ Incorrect
Inorder traversal visits left subtree, then node, then right subtree, producing sorted keys.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' counts values less than threshold.
Using wrong variable names causes errors.
✗ Incorrect
We iterate over entries, unpack each entry, and count values greater than threshold.