Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start searching for floor value in BST.
DSA Java
public int floor(Node root, int key) {
if (root == null) return -1;
if (root.data == [1]) return root.data;
// rest of code omitted
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing root.data with null
Returning 0 instead of key
Using wrong variable in comparison
✗ Incorrect
We check if the current node's data equals the key to return it as floor.
2fill in blank
mediumComplete the code to move left in BST when root data is greater than key.
DSA Java
if (root.data > key) { return [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Searching right subtree instead of left
Returning root.data directly
Returning key without recursion
✗ Incorrect
If root data is greater than key, floor must be in left subtree.
3fill in blank
hardFix the error in code to find floor when root data is less than key.
DSA Java
if (root.data < key) { int floorValue = floor(root.right, key); return floorValue [1] root.data ? floorValue : root.data; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>'
Using '==' which is equality check
Using '>=' which may cause wrong logic
✗ Incorrect
We return the greater value between floorValue and root.data as floor.
4fill in blank
hardFill both blanks to start ceil function and check if root data equals key.
DSA Java
public int ceil(Node root, int key) {
if (root == [1]) return -1;
if (root.data == [2]) return root.data;
// rest of code omitted
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking root == 0 instead of null
Comparing root.data with root instead of key
Returning wrong default value
✗ Incorrect
We check if root is null and if root data equals key for ceil.
5fill in blank
hardFill all three blanks to complete ceil logic when root data is less than key.
DSA Java
if (root.data < [1]) { return [2]; } else { int ceilValue = ceil(root.left, [3]); // rest omitted }
Attempts:
3 left
💡 Hint
Common Mistakes
Using root.data instead of key in comparison
Returning root.data when root.data < key
Passing wrong argument in recursive call
✗ Incorrect
If root data < key, ceil is in right subtree; else search left subtree.
