Challenge - 5 Problems
BST Max Finder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find the maximum element in a BST
What is the output of the following Go code that finds the maximum element in a binary search tree?
DSA Go
package main import "fmt" type Node struct { value int left, right *Node } func findMax(root *Node) int { current := root for current.right != nil { current = current.right } return current.value } func main() { root := &Node{value: 10} root.left = &Node{value: 5} root.right = &Node{value: 20} root.right.left = &Node{value: 15} root.right.right = &Node{value: 25} fmt.Println(findMax(root)) }
Attempts:
2 left
💡 Hint
In a BST, the maximum value is found by going as far right as possible.
✗ Incorrect
The function findMax traverses the tree by moving to the right child until it reaches the rightmost node, which holds the maximum value 25.
❓ Predict Output
intermediate2:00remaining
Output of findMax with single node BST
What will be printed when findMax is called on a BST with only one node?
DSA Go
package main import "fmt" type Node struct { value int left, right *Node } func findMax(root *Node) int { current := root for current.right != nil { current = current.right } return current.value } func main() { root := &Node{value: 42} fmt.Println(findMax(root)) }
Attempts:
2 left
💡 Hint
If there is only one node, that node is the maximum.
✗ Incorrect
Since the root has no right child, the loop does not run and the root's value 42 is returned.
🔧 Debug
advanced2:00remaining
Identify the error in findMax function
What error will the following Go code produce when findMax is called with a nil root?
DSA Go
package main import "fmt" type Node struct { value int left, right *Node } func findMax(root *Node) int { current := root for current.right != nil { current = current.right } return current.value } func main() { var root *Node = nil fmt.Println(findMax(root)) }
Attempts:
2 left
💡 Hint
Accessing fields of a nil pointer causes a runtime panic in Go.
✗ Incorrect
The function tries to access current.right when current is nil, causing a nil pointer dereference runtime error.
🧠 Conceptual
advanced2:00remaining
Why does findMax traverse right children in a BST?
In a binary search tree, why does the findMax function move only to the right child nodes to find the maximum element?
Attempts:
2 left
💡 Hint
Recall the BST property about left and right children values.
✗ Incorrect
In a BST, left children have smaller values and right children have larger values than their parent nodes, so the maximum is found by going right.
🚀 Application
expert3:00remaining
Find maximum in BST after insertion
Given the following BST and the insertion of a new node with value 30, what will be the maximum element after insertion?
DSA Go
package main import "fmt" type Node struct { value int left, right *Node } func insert(root *Node, val int) *Node { if root == nil { return &Node{value: val} } if val < root.value { root.left = insert(root.left, val) } else { root.right = insert(root.right, val) } return root } func findMax(root *Node) int { current := root for current.right != nil { current = current.right } return current.value } func main() { root := &Node{value: 10} root = insert(root, 5) root = insert(root, 20) root = insert(root, 25) root = insert(root, 15) root = insert(root, 30) fmt.Println(findMax(root)) }
Attempts:
2 left
💡 Hint
After inserting 30, it becomes the rightmost node.
✗ Incorrect
The new node with value 30 is inserted as the right child of 25, making 30 the maximum element.