Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the maximum element in a BST by moving to the right child.
DSA Go
func findMax(root *Node) int {
current := root
for current.[1] != nil {
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 traverse.
Trying to access a non-existent 'parent' field.
✗ 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 handle the case when the BST is empty before finding the maximum element.
DSA Go
func findMax(root *Node) int {
if [1] == nil {
return -1 // or an error value
}
current := root
for current.right != nil {
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 for nil.
Not handling the empty tree case.
✗ Incorrect
We check if the root itself is nil to handle an empty tree before proceeding.
3fill in blank
hardFix the error in the loop condition to correctly traverse the BST to find the maximum element.
DSA Go
func findMax(root *Node) int {
current := root
for current.[1] != nil {
current = current.[1]
}
return current.value
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different fields in condition and assignment.
Using 'left' instead of 'right' in the loop.
✗ Incorrect
The loop must check and move to the right child to find the maximum element in a BST.
4fill in blank
hardFill both blanks to complete the recursive function to find the maximum element in a BST.
DSA Go
func findMaxRecursive(root *Node) int {
if root == nil || root.[1] == nil {
return root.value
}
return findMaxRecursive(root.[2])
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' for traversal.
Checking wrong child pointers.
✗ Incorrect
The recursive function moves to the right child until it reaches the rightmost node, which is the maximum.
5fill in blank
hardFill all three blanks to complete the iterative function that finds the maximum element and returns its value.
DSA Go
func findMaxIterative(root *Node) int {
if root == nil {
return -1
}
current := root
for current.[1] != nil {
current = current.[2]
}
return current.[3]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the node instead of its value.
Using left child instead of right child for traversal.
✗ Incorrect
The function moves right until no right child exists, then returns the value of that node.