Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the maximum value in a BST by moving right.
DSA Javascript
function findMax(root) {
if (!root) return null;
let current = root;
while (current.[1] !== null) {
current = current.right;
}
return current.value;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' to find the maximum.
Not checking if the root is null before starting.
✗ Incorrect
In a BST, the maximum element is found by moving to the right child until there is no right child left.
2fill in blank
mediumComplete the code to return null if the BST is empty.
DSA Javascript
function findMax(root) {
if ([1]) return null;
let current = root;
while (current.right !== null) {
current = current.right;
}
return current.value;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking root.value instead of root itself.
Using root.left to check if tree is empty.
✗ Incorrect
Checking if root is falsy (!root) covers null or undefined, meaning the tree is empty.
3fill in blank
hardFix the error in the loop condition to correctly traverse the BST to find the maximum.
DSA Javascript
function findMax(root) {
if (!root) return null;
let current = root;
while (current.[1]) {
current = current.right;
}
return current.value;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using current.left in the loop condition.
Checking current.value instead of current.right.
✗ Incorrect
The loop should continue while current.right exists to move right until the maximum is found.
4fill in blank
hardFill both blanks to complete the function that finds the maximum value in a BST.
DSA Javascript
function findMax(root) {
if ([1]) return null;
let current = root;
while (current.[2] !== null) {
current = current.right;
}
return current.value;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' in the loop.
Checking 'root === null' instead of '!root'.
✗ Incorrect
Check if root is falsy to handle empty tree, then move right while right child exists to find max.
5fill in blank
hardFill all three blanks to complete the function that finds the maximum value in a BST safely.
DSA Javascript
function findMax([1]) { if ([2]) return null; let current = root; while (current.[3] !== null) { current = current.right; } return current.value; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'node' instead of 'root' as parameter.
Not checking if root is null or undefined.
Moving left instead of right in the loop.
✗ Incorrect
The function takes 'root' as parameter, checks if root is falsy, then moves right to find max.