Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the minimum value in the BST.
DSA Go
func findMin(root *Node) int {
current := root
for current.[1] != nil {
current = current.left
}
return current.val
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' to traverse.
Trying to access a non-existent 'parent' pointer.
✗ Incorrect
In a BST, the minimum element is found by going to the left child repeatedly until there is no left child.
2fill in blank
mediumComplete the code to handle the case when the BST is empty.
DSA Go
func findMin(root *Node) int {
if root == [1] {
return -1 // or error code
}
current := root
for current.left != nil {
current = current.left
}
return current.val
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking root == 0 instead of root == nil.
Not handling empty tree case.
✗ Incorrect
If the root is nil, it means the tree is empty, so we return an error code or -1.
3fill in blank
hardFix the error in the loop condition to correctly find the minimum element.
DSA Go
func findMin(root *Node) int {
current := root
for current.[1] != nil {
current = current.left
}
return current.val
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' in the loop condition.
Checking the wrong child pointer.
✗ Incorrect
The loop should continue while current.left is NOT nil, so the condition must check for != nil, not == nil.
4fill in blank
hardFill both blanks to complete the recursive function to find the minimum element.
DSA Go
func findMin(root *Node) int {
if root == [1] {
return -1
}
if root.[2] == nil {
return root.val
}
return findMin(root.left)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking root.right instead of root.left.
Not handling nil root case.
✗ Incorrect
Check if root is nil first, then if root.left is nil, return root.val; else recurse left.
5fill in blank
hardFill all three blanks to create a function that returns the minimum node's value or -1 if empty.
DSA Go
func findMin(root *Node) int {
if root == [1] {
return -1
}
current := root
for current.[2] != [3] {
current = current.left
}
return current.val
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' in the loop.
Comparing to wrong nil pointer.
✗ Incorrect
Check if root is nil, then loop while current.left is not nil, moving left to find minimum.