Challenge - 5 Problems
BST Inorder Predecessor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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") } }
Attempts:
2 left
💡 Hint
Think about the largest value smaller than the key in the BST.
✗ Incorrect
The inorder predecessor of 15 is the largest node smaller than 15. In this BST, 10 is the largest value less than 15.
🧠 Conceptual
intermediate1:00remaining
Understanding Inorder Predecessor in BST
In a Binary Search Tree (BST), what does the inorder predecessor of a node represent?
Attempts:
2 left
💡 Hint
Inorder traversal visits nodes in ascending order.
✗ Incorrect
Inorder predecessor is the node visited just before the given node in inorder traversal, which means it has the largest value smaller than the given node.
🔧 Debug
advanced2: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") } }
Attempts:
2 left
💡 Hint
Check how the predecessor is found when the node has a left subtree.
✗ Incorrect
The code correctly finds the inorder predecessor by going to the left subtree and then rightmost node. For 10, predecessor is 5.
📝 Syntax
advanced1: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 }
Attempts:
2 left
💡 Hint
Look for missing closing braces or misplaced else statements.
✗ Incorrect
Option A misses the closing brace for the if block before else, causing a syntax error.
🚀 Application
expert2: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 }
Attempts:
2 left
💡 Hint
Trace the path from root to the node and count nodes visited.
✗ Incorrect
The function visits nodes 4, 3, and 5 in the path, updating predecessor accordingly. Total nodes visited are 4.