0
0
DSA Goprogramming~20 mins

BST Search Operation in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of BST Search for Existing Value
What is the output of the following Go code that searches for the value 15 in a BST?
DSA Go
package main
import "fmt"
type Node struct {
    val   int
    left  *Node
    right *Node
}
func searchBST(root *Node, target int) *Node {
    if root == nil || root.val == target {
        return root
    }
    if target < root.val {
        return searchBST(root.left, target)
    }
    return searchBST(root.right, target)
}
func main() {
    root := &Node{val: 10}
    root.left = &Node{val: 5}
    root.right = &Node{val: 20}
    root.right.left = &Node{val: 15}
    result := searchBST(root, 15)
    if result != nil {
        fmt.Println(result.val)
    } else {
        fmt.Println("Not found")
    }
}
A15
B20
C5
DNot found
Attempts:
2 left
💡 Hint
Trace the search path comparing target with node values.
Predict Output
intermediate
2:00remaining
Output of BST Search for Non-Existing Value
What is the output of the following Go code that searches for the value 17 in a BST?
DSA Go
package main
import "fmt"
type Node struct {
    val   int
    left  *Node
    right *Node
}
func searchBST(root *Node, target int) *Node {
    if root == nil || root.val == target {
        return root
    }
    if target < root.val {
        return searchBST(root.left, target)
    }
    return searchBST(root.right, target)
}
func main() {
    root := &Node{val: 10}
    root.left = &Node{val: 5}
    root.right = &Node{val: 20}
    root.right.left = &Node{val: 15}
    result := searchBST(root, 17)
    if result != nil {
        fmt.Println(result.val)
    } else {
        fmt.Println("Not found")
    }
}
A15
B5
C20
DNot found
Attempts:
2 left
💡 Hint
Check if the value 17 exists in the tree by following the search path.
🧠 Conceptual
advanced
1:30remaining
Understanding BST Search Path
In a BST, which path does the search algorithm follow to find a target value?
AGo left if target is less than current node, else go right
BGo right if target is less than current node, else go left
CAlways go left child regardless of target value
DSearch both left and right subtrees simultaneously
Attempts:
2 left
💡 Hint
Think about how BST property orders values.
🔧 Debug
advanced
2:00remaining
Identify the Bug in BST Search Code
What error will the following Go code produce when searching for a value in a BST?
DSA Go
package main
import "fmt"
type Node struct {
    val   int
    left  *Node
    right *Node
}
func searchBST(root *Node, target int) *Node {
    if root == nil {
        return nil
    }
    if root.val == target {
        return root
    }
    if target > root.val {
        return searchBST(root.left, target)
    }
    return searchBST(root.right, target)
}
func main() {
    root := &Node{val: 10}
    root.left = &Node{val: 5}
    root.right = &Node{val: 20}
    result := searchBST(root, 5)
    if result != nil {
        fmt.Println(result.val)
    } else {
        fmt.Println("Not found")
    }
}
APrints 20
BPrints 5
CPrints "Not found"
DCompilation error
Attempts:
2 left
💡 Hint
Check the direction of recursive calls for target > root.val.
🚀 Application
expert
2:30remaining
Number of Nodes Visited in BST Search
Given a BST with nodes inserted in this order: 10, 5, 15, 3, 7, 12, 18. How many nodes will be visited when searching for the value 7?
A4
B3
C2
D5
Attempts:
2 left
💡 Hint
Trace the search path from root to the target node counting nodes visited.