0
0
DSA Goprogramming~20 mins

BST Inorder Predecessor in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Inorder Predecessor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Inorder Predecessor Function in BST
What is the output of the following Go code that finds the inorder predecessor of a node with value 15 in a BST?
DSA Go
package main

import "fmt"

type Node struct {
    Val   int
    Left  *Node
    Right *Node
}

func inorderPredecessor(root *Node, key int) *Node {
    var predecessor *Node
    current := root
    for current != nil {
        if key <= current.Val {
            current = current.Left
        } else {
            predecessor = current
            current = current.Right
        }
    }
    return predecessor
}

func main() {
    root := &Node{20, nil, nil}
    root.Left = &Node{10, nil, nil}
    root.Right = &Node{30, nil, nil}
    root.Left.Left = &Node{5, nil, nil}
    root.Left.Right = &Node{15, nil, nil}

    pred := inorderPredecessor(root, 15)
    if pred != nil {
        fmt.Println(pred.Val)
    } else {
        fmt.Println("nil")
    }
}
A5
B10
C15
Dnil
Attempts:
2 left
💡 Hint
Think about the largest value smaller than the key in the BST.
🧠 Conceptual
intermediate
1:00remaining
Understanding Inorder Predecessor in BST
In a Binary Search Tree (BST), what does the inorder predecessor of a node represent?
AThe node with the smallest value greater than the given node
BThe left child of the given node
CThe node with the largest value smaller than the given node
DThe right child of the given node
Attempts:
2 left
💡 Hint
Inorder traversal visits nodes in ascending order.
🔧 Debug
advanced
2:00remaining
Identify the Error in Inorder Predecessor Code
What error will the following Go code produce when trying to find the inorder predecessor of 10 in the BST?
DSA Go
package main

import "fmt"

type Node struct {
    Val   int
    Left  *Node
    Right *Node
}

func inorderPredecessor(root *Node, key int) *Node {
    var predecessor *Node
    current := root
    for current != nil {
        if key < current.Val {
            current = current.Left
        } else if key > current.Val {
            predecessor = current
            current = current.Right
        } else {
            if current.Left != nil {
                temp := current.Left
                for temp.Right != nil {
                    temp = temp.Right
                }
                predecessor = temp
            }
            break
        }
    }
    return predecessor
}

func main() {
    root := &Node{20, nil, nil}
    root.Left = &Node{10, nil, nil}
    root.Right = &Node{30, nil, nil}
    root.Left.Left = &Node{5, nil, nil}
    root.Left.Right = &Node{15, nil, nil}

    pred := inorderPredecessor(root, 10)
    if pred != nil {
        fmt.Println(pred.Val)
    } else {
        fmt.Println("nil")
    }
}
AIt causes a runtime panic
BIt prints 15
CIt prints nil
DIt prints 5
Attempts:
2 left
💡 Hint
Check how the predecessor is found when the node has a left subtree.
📝 Syntax
advanced
1:30remaining
Identify Syntax Error in BST Inorder Predecessor Code
Which option contains a syntax error in the Go code for finding the inorder predecessor in a BST?
DSA Go
func inorderPredecessor(root *Node, key int) *Node {
    var predecessor *Node
    current := root
    for current != nil {
        if key <= current.Val {
            current = current.Left
        } else {
            predecessor = current
            current = current.Right
        }
    }
    return predecessor
}
Afunc inorderPredecessor(root *Node, key int) *Node { var predecessor *Node current := root for current != nil { if key <= current.Val { current = current.Left else { predecessor = current current = current.Right } } } return predecessor }
B} rossecederp nruter } } thgiR.tnerruc = tnerruc tnerruc = rossecederp { esle } tfeL.tnerruc = tnerruc { laV.tnerruc =< yek fi { lin =! tnerruc rof toor =: tnerruc edoN* rossecederp rav { edoN* )tni yek ,edoN* toor(rossecederPredroni cnuf
Cfunc inorderPredecessor(root *Node, key int) *Node { var predecessor *Node current := root for current != nil { if key <= current.Val { current = current.Left } else { predecessor = current current = current.Right } } return predecessor }
Dunc inorderPredecessor(root *Node, key int) *Node { var predecessor *Node current := root for current != nil { if key <= current.Val { current = current.Left } else { predecessor = current current = current.Right } } return predecessor }
Attempts:
2 left
💡 Hint
Look for missing closing braces or misplaced else statements.
🚀 Application
expert
2:30remaining
Number of Nodes Visited to Find Inorder Predecessor
Given a balanced BST with 7 nodes (values 1 to 7), how many nodes will the inorderPredecessor function visit to find the predecessor of node with value 6?
DSA Go
func inorderPredecessor(root *Node, key int) *Node {
    var predecessor *Node
    current := root
    for current != nil {
        if key <= current.Val {
            current = current.Left
        } else {
            predecessor = current
            current = current.Right
        }
    }
    return predecessor
}
A4
B6
C5
D3
Attempts:
2 left
💡 Hint
Trace the path from root to the node and count nodes visited.