0
0
DSA Goprogramming~20 mins

BST Property and Why It Matters in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Inserting Nodes in BST
What is the printed state of the BST after inserting the values 10, 5, 15, 3, 7 in that order? The BST property is maintained during insertion.
DSA Go
package main

import "fmt"

type Node struct {
    val   int
    left  *Node
    right *Node
}

func insert(root *Node, val int) *Node {
    if root == nil {
        return &Node{val: val}
    }
    if val < root.val {
        root.left = insert(root.left, val)
    } else {
        root.right = insert(root.right, val)
    }
    return root
}

func inorder(root *Node) {
    if root == nil {
        return
    }
    inorder(root.left)
    fmt.Print(root.val, " -> ")
    inorder(root.right)
}

func main() {
    var root *Node
    vals := []int{10, 5, 15, 3, 7}
    for _, v := range vals {
        root = insert(root, v)
    }
    inorder(root)
    fmt.Print("null\n")
}
A3 -> 5 -> 7 -> 10 -> 15 -> null
B10 -> 5 -> 3 -> 7 -> 15 -> null
C15 -> 10 -> 7 -> 5 -> 3 -> null
D3 -> 7 -> 5 -> 10 -> 15 -> null
Attempts:
2 left
💡 Hint
Remember that inorder traversal of a BST prints values in ascending order.
🧠 Conceptual
intermediate
1:30remaining
Why BST Property Matters for Search Efficiency
Why does maintaining the BST property help in searching for a value efficiently?
ABecause it sorts values after every insertion to speed up search.
BBecause it stores all values in a linked list for quick access.
CBecause it allows skipping half of the tree at each step, similar to binary search.
DBecause it uses hashing to find values instantly.
Attempts:
2 left
💡 Hint
Think about how binary search works on sorted arrays.
🔧 Debug
advanced
2:00remaining
Identify the Error in BST Insertion Code
What error will this Go code produce when inserting values into a BST?
DSA Go
func insert(root *Node, val int) *Node {
    if root == nil {
        return &Node{val: val}
    }
    if val <= root.val {
        root.left = insert(root.left, val)
    } else {
        root.right = insert(root.right, val)
    }
    return root
}

// Node struct is defined as usual.
ARuntime panic due to nil pointer dereference.
BStack overflow due to infinite recursion on duplicates.
CCompilation error due to missing return statement.
DNo error; duplicates go to left subtree.
Attempts:
2 left
💡 Hint
Check how duplicates are handled in the code.
Predict Output
advanced
2:30remaining
Output After Deleting a Node in BST
Given a BST with nodes 20, 10, 30, 5, 15, what is the inorder traversal output after deleting node 10?
DSA Go
package main

import "fmt"

type Node struct {
    val   int
    left  *Node
    right *Node
}

func insert(root *Node, val int) *Node {
    if root == nil {
        return &Node{val: val}
    }
    if val < root.val {
        root.left = insert(root.left, val)
    } else {
        root.right = insert(root.right, val)
    }
    return root
}

func minValueNode(node *Node) *Node {
    current := node
    for current.left != nil {
        current = current.left
    }
    return current
}

func deleteNode(root *Node, val int) *Node {
    if root == nil {
        return root
    }
    if val < root.val {
        root.left = deleteNode(root.left, val)
    } else if val > root.val {
        root.right = deleteNode(root.right, val)
    } else {
        if root.left == nil {
            return root.right
        } else if root.right == nil {
            return root.left
        }
        temp := minValueNode(root.right)
        root.val = temp.val
        root.right = deleteNode(root.right, temp.val)
    }
    return root
}

func inorder(root *Node) {
    if root == nil {
        return
    }
    inorder(root.left)
    fmt.Print(root.val, " -> ")
    inorder(root.right)
}

func main() {
    var root *Node
    vals := []int{20, 10, 30, 5, 15}
    for _, v := range vals {
        root = insert(root, v)
    }
    root = deleteNode(root, 10)
    inorder(root)
    fmt.Print("null\n")
}
A5 -> 15 -> 20 -> 30 -> null
B5 -> 10 -> 15 -> 20 -> 30 -> null
C15 -> 5 -> 20 -> 30 -> null
D5 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
When deleting a node with two children, replace it with the smallest node in the right subtree.
🧠 Conceptual
expert
1:30remaining
Effect of Violating BST Property on Search
If a binary tree does not maintain the BST property, what is the impact on search operation efficiency?
ASearch becomes constant time because nodes are unordered.
BSearch becomes linear time, similar to searching an unsorted list.
CSearch remains logarithmic time due to tree structure.
DSearch cannot be performed at all.
Attempts:
2 left
💡 Hint
Think about how the BST property helps reduce search space.