Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start searching for the floor value in the BST.
DSA Typescript
function floorInBST(root: TreeNode | null, key: number): number | null {
let floor: number | null = null;
let current = [1];
while (current !== null) {
if (current.val === key) {
return current.val;
} else if (current.val > key) {
current = current.left;
} else {
floor = current.val;
current = current.right;
}
}
return floor;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from null instead of root.
Using the key as the starting node.
✗ Incorrect
We start searching from the root node of the BST.
2fill in blank
mediumComplete the code to update the ceil value while searching in the BST.
DSA Typescript
function ceilInBST(root: TreeNode | null, key: number): number | null {
let ceil: number | null = null;
let current = root;
while (current !== null) {
if (current.val === key) {
return current.val;
} else if (current.val < key) {
current = current.right;
} else {
[1] = current.val;
current = current.left;
}
}
return ceil;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Updating floor instead of ceil.
Not updating ceil at all.
✗ Incorrect
When current node value is greater than key, update ceil to current.val.
3fill in blank
hardFix the error in the floor function to correctly return null when no floor exists.
DSA Typescript
function floorInBST(root: TreeNode | null, key: number): number | null {
let floor: number | null = null;
let current = root;
while (current !== null) {
if (current.val === key) {
return current.val;
} else if (current.val > key) {
current = current.left;
} else {
floor = current.val;
current = current.right;
}
}
return [1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 or -1 which may not be correct floor.
Returning key which is unrelated.
✗ Incorrect
Return the floor variable which is null if no floor was found.
4fill in blank
hardFill both blanks to complete the ceil function that returns null if no ceil exists.
DSA Typescript
function ceilInBST(root: TreeNode | null, key: number): number | null {
let ceil: number | null = null;
let current = root;
while (current !== null) {
if (current.val === key) {
return current.val;
} else if (current.val < key) {
current = current.[1];
} else {
ceil = current.val;
current = current.[2];
}
}
return ceil;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right pointers.
Using parent or root instead of child pointers.
✗ Incorrect
When current.val < key, move right; when current.val > key, move left.
5fill in blank
hardFill both blanks to create a function that returns both floor and ceil in one pass.
DSA Typescript
function floorAndCeilInBST(root: TreeNode | null, key: number): { floor: number | null; ceil: number | null } {
let floor: number | null = null;
let ceil: number | null = null;
let current = root;
while (current !== null) {
if (current.val === key) {
floor = current.val;
ceil = current.val;
break;
} else if (current.val < key) {
floor = current.val;
current = current.[1];
} else {
ceil = current.val;
current = current.[2];
}
}
return { floor: floor, ceil: ceil };
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right pointers.
Adding extra values in return statement.
✗ Incorrect
Move right if current.val < key, left if greater, and return floor and ceil as is.