Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the floor value in a BST.
DSA Python
def floor_in_bst(root, key): floor_val = None current = root while current: if current.val == key: return current.val elif current.val > key: current = current.[1] else: floor_val = current.val current = current.right return floor_val
Attempts:
3 left
💡 Hint
Common Mistakes
Going right when current value is greater than key causes wrong floor value.
Returning None too early without checking left subtree.
✗ Incorrect
To find the floor, if current node's value is greater than key, move to left child to find smaller values.
2fill in blank
mediumComplete the code to find the ceil value in a BST.
DSA Python
def ceil_in_bst(root, key): ceil_val = None current = root while current: if current.val == key: return current.val elif current.val < key: current = current.[1] else: ceil_val = current.val current = current.left return ceil_val
Attempts:
3 left
💡 Hint
Common Mistakes
Going left when current value is smaller than key causes wrong ceil value.
Returning None too early without checking right subtree.
✗ Incorrect
If current node's value is less than key, move right to find bigger values for ceil.
3fill in blank
hardFix the error in the code to correctly find floor in BST.
DSA Python
def floor_in_bst(root, key): floor_val = None current = root while current: if current.val == key: return current.val elif current.val < key: floor_val = current.val current = current.[1] else: current = current.left return floor_val
Attempts:
3 left
💡 Hint
Common Mistakes
Going left when current value is less than key causes missing closer floor values.
Not updating floor_val correctly.
✗ Incorrect
When current value is less than key, move right to find a closer floor value.
4fill in blank
hardFill both blanks to complete the code that finds ceil in BST.
DSA Python
def ceil_in_bst(root, key): ceil_val = None current = root while current: if current.val == key: return current.val elif current.val [1] key: current = current.[2] else: ceil_val = current.val current = current.left return ceil_val
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operator causes wrong traversal.
Going left instead of right when current value is less than key.
✗ Incorrect
If current value is less than key, go right to find bigger values for ceil.
5fill in blank
hardFill all three blanks to complete the code that finds floor in BST.
DSA Python
def floor_in_bst(root, key): floor_val = None current = root while current: if current.val == key: return current.val elif current.val [1] key: floor_val = current.val current = current.[2] else: current = current.[3] return floor_val
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right child traversal.
Not updating floor_val when current value is less than key.
✗ Incorrect
If current value is less than key, update floor and go right; else go left.
