Bird
Raised Fist0
DSA Goprogramming~20 mins

BST Search Operation in DSA Go - Practice Problems & Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the search operation in a Binary Search Tree (BST) do?
easy
A. Finds whether a given value exists in the BST
B. Deletes a node from the BST
C. Inserts a new node into the BST
D. Traverses all nodes in the BST

Solution

  1. Step 1: Understand the purpose of BST search

    The search operation is designed to check if a specific value is present in the BST.
  2. Step 2: Differentiate from other operations

    Insertion adds nodes, deletion removes nodes, and traversal visits nodes. Search only checks for existence.
  3. Final Answer:

    Finds whether a given value exists in the BST -> Option A
  4. Quick Check:

    Search = Find value [OK]
Hint: Search checks presence, not insertion or deletion [OK]
Common Mistakes:
  • Confusing search with insert or delete
  • Thinking search returns the node always
  • Assuming search traverses all nodes
2. Which of the following is the correct Go function signature for a BST search method that returns a boolean indicating if a value exists?
easy
A. func Search(val int) *Node
B. func (root *Node) Search(val int) bool
C. func (root Node) Search(val string) bool
D. func Search(root Node, val int) int

Solution

  1. Step 1: Identify method receiver and return type

    BST search is usually a method on a pointer to Node and returns bool for found or not.
  2. Step 2: Check parameter types and return type

    func (root *Node) Search(val int) bool uses pointer receiver, int parameter, and bool return which matches the expected signature.
  3. Final Answer:

    func (root *Node) Search(val int) bool -> Option B
  4. Quick Check:

    Pointer receiver + bool return = func (root *Node) Search(val int) bool [OK]
Hint: Search returns bool, uses pointer receiver for tree nodes [OK]
Common Mistakes:
  • Using value receiver instead of pointer
  • Wrong parameter or return types
  • Confusing method vs function signatures
3. Given the BST below and the Go code snippet, what will be the output? BST structure: ``` 10 / \ 5 15 / 3 ``` Go code:
found := root.Search(3)
fmt.Println(found)
medium
A. true
B. false
C. nil
D. runtime error

Solution

  1. Step 1: Trace the search for value 3 in the BST

    Starting at root 10, 3 < 10, go left to 5; 3 < 5, go left to 3; found node with value 3.
  2. Step 2: Understand the returned value and print output

    Search returns true because 3 exists; printing true outputs 'true'.
  3. Final Answer:

    true -> Option A
  4. Quick Check:

    3 found in BST = true [OK]
Hint: Follow BST left/right rules to find value [OK]
Common Mistakes:
  • Assuming search returns node instead of bool
  • Confusing true/false output
  • Ignoring BST structure when tracing
4. The following Go code for BST search has a bug. What is the bug?
func (root *Node) Search(val int) bool {
    if root == nil {
        return false
    }
    if root.Value == val {
        return true
    }
    if val < root.Value {
        return root.Search(val)
    } else {
        return root.Search(val)
    }
}
medium
A. Base case for nil root is missing
B. Uses value receiver instead of pointer
C. Returns int instead of bool
D. Recursive calls do not move to left or right child nodes

Solution

  1. Step 1: Analyze recursive calls in search method

    Both recursive calls use root.Search(val) without moving to root.Left or root.Right, causing infinite recursion.
  2. Step 2: Identify correct recursive calls

    Should call root.Left.Search(val) if val < root.Value, else root.Right.Search(val) to move down the tree.
  3. Final Answer:

    Recursive calls do not move to left or right child nodes -> Option D
  4. Quick Check:

    Recursive calls must move down tree [OK]
Hint: Recursive calls must go to left or right child [OK]
Common Mistakes:
  • Calling search on same node causing infinite loop
  • Missing base case for nil
  • Wrong return types
5. You want to search for a value in a BST but also count how many nodes you visited during the search. Which modification to the search function is best?
hard
A. Use a global variable to count nodes visited during search
B. Return the node instead of bool and count nodes outside the function
C. Add a counter parameter passed by pointer and increment it on each node visited
D. Modify the BST to store counts in each node

Solution

  1. Step 1: Understand the need to count nodes during search

    Counting nodes visited requires tracking each node checked during recursion.
  2. Step 2: Choose method to count nodes visited

    Passing a pointer to a counter variable and incrementing it at each node visit allows accurate counting without side effects.
  3. Final Answer:

    Add a counter parameter passed by pointer and increment it on each node visited -> Option C
  4. Quick Check:

    Pass counter pointer to track visits [OK]
Hint: Pass counter pointer to track nodes visited [OK]
Common Mistakes:
  • Using global variables causing side effects
  • Counting outside search missing recursive visits
  • Changing BST structure unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard