Challenge - 5 Problems
BST Minimum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find minimum element in a BST
What is the output of this Go code that finds the minimum element in a binary search tree?
DSA Go
package main import "fmt" type Node struct { Val int Left *Node Right *Node } func findMin(root *Node) int { current := root for current.Left != nil { current = current.Left } return current.Val } func main() { root := &Node{Val: 10} root.Left = &Node{Val: 5} root.Right = &Node{Val: 15} root.Left.Left = &Node{Val: 2} root.Left.Right = &Node{Val: 7} fmt.Println(findMin(root)) }
Attempts:
2 left
💡 Hint
The minimum element in a BST is the leftmost node.
✗ Incorrect
The function moves left until it finds the leftmost node, which has the smallest value. Here, that value is 2.
❓ Predict Output
intermediate2:00remaining
Minimum element in a BST with only right children
What will the output be when finding the minimum element in this BST where all nodes only have right children?
DSA Go
package main import "fmt" type Node struct { Val int Left *Node Right *Node } func findMin(root *Node) int { current := root for current.Left != nil { current = current.Left } return current.Val } func main() { root := &Node{Val: 3} root.Right = &Node{Val: 6} root.Right.Right = &Node{Val: 9} fmt.Println(findMin(root)) }
Attempts:
2 left
💡 Hint
If there is no left child, the root is the minimum.
✗ Incorrect
Since no node has a left child, the root node value 3 is the minimum.
🔧 Debug
advanced2:00remaining
Identify the error in this BST minimum finder
What error will this Go code produce when trying to find the minimum element in a BST?
DSA Go
package main import "fmt" type Node struct { Val int Left *Node Right *Node } func findMin(root *Node) int { current := root for current != nil { current = current.Left } return current.Val } func main() { root := &Node{Val: 8} root.Left = &Node{Val: 3} root.Left.Left = &Node{Val: 1} fmt.Println(findMin(root)) }
Attempts:
2 left
💡 Hint
Check what happens after the loop ends.
✗ Incorrect
The loop continues until current is nil, then tries to access current.Val causing a nil pointer dereference.
🧠 Conceptual
advanced1:30remaining
Why does the minimum element lie in the left subtree in a BST?
Which option best explains why the minimum element in a binary search tree is always found by going left until no more left child exists?
Attempts:
2 left
💡 Hint
Recall the BST property about left and right children values.
✗ Incorrect
By definition, in a BST, left children have smaller values than their parents, so the smallest value is found by going left as far as possible.
❓ Predict Output
expert2:30remaining
Output of recursive minimum finder in BST
What is the output of this recursive Go function that finds the minimum element in a BST?
DSA Go
package main import "fmt" type Node struct { Val int Left *Node Right *Node } func findMinRecursive(root *Node) int { if root.Left == nil { return root.Val } return findMinRecursive(root.Left) } func main() { root := &Node{Val: 20} root.Left = &Node{Val: 10} root.Left.Left = &Node{Val: 5} root.Left.Left.Left = &Node{Val: 2} root.Right = &Node{Val: 30} fmt.Println(findMinRecursive(root)) }
Attempts:
2 left
💡 Hint
The function calls itself on the left child until no left child exists.
✗ Incorrect
The recursion goes down the left children until it reaches the leftmost node with value 2, which it returns.