Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the maximum value in the BST.
DSA Java
public int findMax(Node root) {
if (root == null) {
return Integer.MIN_VALUE;
}
Node current = root;
while (current.[1] != null) {
current = current.right;
}
return current.data;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' to find maximum.
Returning root data without traversal.
✗ Incorrect
In a BST, the maximum element is found by going to the rightmost node. So, we keep moving to the right child until it is null.
2fill in blank
mediumComplete the code to handle the case when the BST is empty.
DSA Java
public int findMax(Node root) {
if (root == [1]) {
return Integer.MIN_VALUE;
}
Node current = root;
while (current.right != null) {
current = current.right;
}
return current.data;
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking if root equals 'root' instead of null.
Not handling empty tree case.
✗ Incorrect
If the root is null, it means the tree is empty, so we return Integer.MIN_VALUE as a signal for no maximum.
3fill in blank
hardFix the error in the loop condition to correctly find the maximum element.
DSA Java
public int findMax(Node root) {
if (root == null) {
return Integer.MIN_VALUE;
}
Node current = root;
while (current.[1] != null) {
current = current.right;
}
return current.data;
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking left child instead of right.
Checking data field in loop condition.
✗ Incorrect
The loop should continue while the right child exists, so we check current.right != null.
4fill in blank
hardFill both blanks to complete the recursive method to find the maximum element.
DSA Java
public int findMax(Node root) {
if (root == null) {
return Integer.MIN_VALUE;
}
if (root.[1] == null) {
return root.data;
}
return findMax(root.[2]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using left child instead of right child.
Returning root data without checking children.
✗ Incorrect
If the right child is null, root is max. Otherwise, recurse on right child.
5fill in blank
hardFill all three blanks to complete the iterative method that finds the maximum element.
DSA Java
public int findMax(Node root) {
if (root == null) {
return [1];
}
Node current = root;
while (current.[2] != null) {
current = current.[3];
}
return current.data;
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of Integer.MIN_VALUE for empty tree.
Moving left instead of right in the loop.
✗ Incorrect
Return Integer.MIN_VALUE if tree is empty. Move right while right child exists to find max.
