0
0
DSA Goprogramming~20 mins

BST Find Maximum Element in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Max Finder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
}
A15
B20
C25
D10
Attempts:
2 left
💡 Hint
In a BST, the maximum value is found by going as far right as possible.
Predict Output
intermediate
2: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))
}
A42
B0
Cnil
Druntime error
Attempts:
2 left
💡 Hint
If there is only one node, that node is the maximum.
🔧 Debug
advanced
2: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))
}
Acompilation error: missing return statement
Bruntime error: invalid memory address or nil pointer dereference
C0
Dprints nothing
Attempts:
2 left
💡 Hint
Accessing fields of a nil pointer causes a runtime panic in Go.
🧠 Conceptual
advanced
2: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?
ABecause left children always have the maximum values
BBecause the tree is balanced and right nodes are visited first
CBecause the root node always contains the maximum value
DBecause in a BST, all right children have values greater than their parent nodes
Attempts:
2 left
💡 Hint
Recall the BST property about left and right children values.
🚀 Application
expert
3: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))
}
A30
B25
C15
D20
Attempts:
2 left
💡 Hint
After inserting 30, it becomes the rightmost node.