Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the floor search from the root node.
DSA C#
public int Floor(Node root, int key) {
Node current = [1];
int floor = -1;
while (current != null) {
if (current.data == key) return current.data;
else if (current.data > key) current = current.left;
else {
floor = current.data;
current = current.right;
}
}
return floor;
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting from null instead of root causes no search.
Starting from root.left or root.right skips part of the tree.
✗ Incorrect
We start searching from the root node to find the floor value in the BST.
2fill in blank
mediumComplete the code to update the ceil value when current node's data is greater than the key.
DSA C#
public int Ceil(Node root, int key) {
Node current = root;
int ceil = -1;
while (current != null) {
if (current.data == key) return current.data;
else if (current.data < key) current = current.right;
else {
ceil = [1];
current = current.left;
}
}
return ceil;
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning ceil to key instead of current.data.
Not updating ceil at all.
✗ Incorrect
When current node's data is greater than key, it is a candidate for ceil, so update ceil to current.data.
3fill in blank
hardFix the error in the floor function where the loop condition is incorrect.
DSA C#
public int Floor(Node root, int key) {
Node current = root;
int floor = -1;
while ([1]) {
if (current.data == key) return current.data;
else if (current.data > key) current = current.left;
else {
floor = current.data;
current = current.right;
}
}
return floor;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'current == null' causes the loop to never run.
Using root in the condition instead of current.
✗ Incorrect
The loop should continue while current node is not null to traverse the BST properly.
4fill in blank
hardFill both blanks to complete the ceil function that returns the smallest value greater than or equal to the key.
DSA C#
public int Ceil(Node root, int key) {
Node current = [1];
int ceil = -1;
while (current != null) {
if (current.data == key) return current.data;
else if (current.data < key) current = [2];
else {
ceil = current.data;
current = current.left;
}
}
return ceil;
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting from null instead of root.
Moving left instead of right when current data is less than key.
✗ Incorrect
Start from root and move right when current data is less than key to find ceil.
5fill in blank
hardFill all three blanks to complete the floor function that finds the greatest value less than or equal to the key.
DSA C#
public int Floor(Node root, int key) {
Node current = [1];
int floor = -1;
while (current != null) {
if (current.data == key) return current.data;
else if (current.data > key) current = [2];
else {
floor = [3];
current = current.right;
}
}
return floor;
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting from null instead of root.
Moving right instead of left when current data is greater than key.
Not updating floor correctly.
✗ Incorrect
Start from root, move left if current data is greater than key, update floor with current data when less than key.
