Challenge - 5 Problems
Kth Smallest Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Kth Smallest Element Function
What is the output of the following Go code that finds the 3rd smallest element in a BST?
DSA Go
package main import "fmt" type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func kthSmallest(root *TreeNode, k int) int { stack := []*TreeNode{} current := root count := 0 for current != nil || len(stack) > 0 { for current != nil { stack = append(stack, current) current = current.Left } current = stack[len(stack)-1] stack = stack[:len(stack)-1] count++ if count == k { return current.Val } current = current.Right } return -1 } func main() { root := &TreeNode{Val: 5} root.Left = &TreeNode{Val: 3} root.Right = &TreeNode{Val: 6} root.Left.Left = &TreeNode{Val: 2} root.Left.Right = &TreeNode{Val: 4} root.Left.Left.Left = &TreeNode{Val: 1} fmt.Println(kthSmallest(root, 3)) }
Attempts:
2 left
💡 Hint
Remember that an in-order traversal of a BST visits nodes in ascending order.
✗ Incorrect
The in-order traversal visits nodes in this order: 1, 2, 3, 4, 5, 6. The 3rd smallest element is 3.
🧠 Conceptual
intermediate1:30remaining
Understanding Kth Smallest Element in BST
Which property of a Binary Search Tree (BST) allows the kth smallest element to be found efficiently using in-order traversal?
Attempts:
2 left
💡 Hint
Think about the order in which nodes are visited during in-order traversal.
✗ Incorrect
In-order traversal of a BST visits nodes in ascending order of their values, which allows us to find the kth smallest element by counting nodes visited.
🔧 Debug
advanced2:00remaining
Identify the Error in Kth Smallest Element Code
What error will the following Go code produce when trying to find the 2nd smallest element in a BST?
DSA Go
package main import "fmt" type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func kthSmallest(root *TreeNode, k int) int { var count int var result int var inorder func(node *TreeNode) inorder = func(node *TreeNode) { if node == nil { return } inorder(node.Left) count++ if count == k { result = node.Val return } inorder(node.Right) } inorder(root) return result } func main() { root := &TreeNode{Val: 3} root.Left = &TreeNode{Val: 1} root.Right = &TreeNode{Val: 4} root.Left.Right = &TreeNode{Val: 2} fmt.Println(kthSmallest(root, 2)) }
Attempts:
2 left
💡 Hint
Check if the recursion stops after finding the kth element.
✗ Incorrect
The recursion does not stop after finding the kth element because the return inside the inner function only exits that call, not the entire traversal. So result is set but traversal continues, and result may be overwritten or default zero returned.
❓ Predict Output
advanced2:00remaining
Output of Modified Kth Smallest Element Code
What is the output of this Go code that finds the 4th smallest element in a BST with duplicate values?
DSA Go
package main import "fmt" type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func kthSmallest(root *TreeNode, k int) int { stack := []*TreeNode{} current := root count := 0 for current != nil || len(stack) > 0 { for current != nil { stack = append(stack, current) current = current.Left } current = stack[len(stack)-1] stack = stack[:len(stack)-1] count++ if count == k { return current.Val } current = current.Right } return -1 } func main() { root := &TreeNode{Val: 5} root.Left = &TreeNode{Val: 3} root.Right = &TreeNode{Val: 6} root.Left.Left = &TreeNode{Val: 3} root.Left.Right = &TreeNode{Val: 4} fmt.Println(kthSmallest(root, 4)) }
Attempts:
2 left
💡 Hint
Duplicates appear in in-order traversal as repeated values.
✗ Incorrect
The in-order traversal visits nodes in order: 3, 3, 4, 5, 6. The 4th smallest element is 4.
🚀 Application
expert2:30remaining
Optimizing Kth Smallest Element Search with Augmented BST
You want to find the kth smallest element in a BST efficiently multiple times. Which data structure modification helps achieve O(log n) time per query?
Attempts:
2 left
💡 Hint
Think about augmenting nodes with extra info to skip parts of the tree.
✗ Incorrect
Storing subtree sizes at each node allows deciding whether to go left, right, or return current node based on k, achieving O(log n) time per query.