A Binary Search Tree is a tree data structure where each node has at most two children. The left child's value is less than the parent's value, and the right child's value is greater than the parent's value.
Click to reveal answer
beginner
In a BST, where is the maximum element located?
The maximum element in a BST is located at the rightmost node because values increase as you move right.
Click to reveal answer
beginner
Why do we keep moving right to find the maximum element in a BST?
Because in a BST, all right children have values greater than their parents, so moving right leads to larger values until the largest is found.
Click to reveal answer
intermediate
What is the time complexity of finding the maximum element in a BST?
The time complexity is O(h), where h is the height of the BST. In the worst case (skewed tree), it can be O(n), and in a balanced tree, it is O(log n).
Click to reveal answer
intermediate
Show the Go code snippet to find the maximum element in a BST.
func findMax(root *Node) *Node {
if root == nil {
return nil
}
current := root
for current.Right != nil {
current = current.Right
}
return current
}
Click to reveal answer
Where do you find the maximum element in a BST?
AAny leaf node
BRightmost node
CRoot node
DLeftmost node
✗ Incorrect
The maximum element is always at the rightmost node in a BST.
What is the first step to find the maximum element in a BST?
AGo to the right child
BGo to the left child
CCheck the root value
DTraverse all nodes
✗ Incorrect
You start from the root and keep moving to the right child to find the maximum.
If a BST has only one node, what is the maximum element?
ANo maximum element
BLeft child
CThe root node itself
DRight child
✗ Incorrect
With only one node, that node is both minimum and maximum.
What is the time complexity to find the maximum element in a balanced BST with n nodes?
AO(n)
BO(1)
CO(n log n)
DO(log n)
✗ Incorrect
In a balanced BST, height is log n, so finding max is O(log n).
What happens if you call findMax on an empty BST?
AReturns nil
BReturns root
CReturns error
DReturns zero
✗ Incorrect
If the BST is empty (root is nil), the function returns nil.
Explain how to find the maximum element in a Binary Search Tree.
Think about the BST property where right children are greater.
You got /4 concepts.
Write a simple Go function to find the maximum element in a BST and explain its steps.
Use a loop to move right until no more right child.
You got /4 concepts.
Practice
(1/5)
1. In a Binary Search Tree (BST), where is the maximum element always located?
easy
A. At any leaf node
B. At the rightmost node
C. At the root node
D. At the leftmost node
Solution
Step 1: Understand BST property
In a BST, all nodes to the right of a node have greater values.
Step 2: Locate maximum element
The maximum element is found by moving to the rightmost node, as it has no greater right child.
Final Answer:
At the rightmost node -> Option B
Quick Check:
Maximum = rightmost node [OK]
Hint: Maximum is always the rightmost node in BST [OK]
Common Mistakes:
Confusing maximum with root node
Thinking maximum is at leftmost node
Assuming maximum can be any leaf
2. Which of the following Go code snippets correctly finds the maximum element in a BST node structure?
easy
A. for current.Left != nil { current = current.Left } return current.Value
B. return root.Value
C. for current.Right != nil { current = current.Right } return current.Value
D. for current != nil { current = current.Left } return current.Value
Solution
Step 1: Check loop condition for maximum
Maximum is found by moving right until no right child exists.
Step 2: Verify code correctness
for current.Right != nil { current = current.Right } return current.Value correctly loops while current.Right is not nil, then returns current.Value.
Final Answer:
for current.Right != nil { current = current.Right } return current.Value -> Option C
Quick Check:
Loop right child until nil = max [OK]
Hint: Loop right child until nil to find max [OK]
Common Mistakes:
Looping left child instead of right
Returning root value without traversal
Loop condition incorrect or infinite
3. What will be the output of the following Go code snippet that finds the maximum element in a BST?
type Node struct {
Value int
Left *Node
Right *Node
}
func findMax(root *Node) int {
current := root
for current.Right != nil {
current = current.Right
}
return current.Value
}
root := &Node{Value: 10}
root.Left = &Node{Value: 5}
root.Right = &Node{Value: 15}
root.Right.Right = &Node{Value: 20}
fmt.Println(findMax(root))
medium
A. 20
B. 15
C. 10
D. 5
Solution
Step 1: Trace the BST structure
Root is 10, right child 15, right-right child 20 (rightmost node).
Step 2: Follow findMax function
Function moves right until current.Right is nil, stopping at node with value 20.
Final Answer:
20 -> Option A
Quick Check:
Rightmost node value = 20 [OK]
Hint: Rightmost node value is maximum [OK]
Common Mistakes:
Returning root value instead of rightmost
Stopping at first right child
Confusing left and right pointers
4. The following Go function is intended to find the maximum element in a BST but contains a bug. What is the bug?
func findMax(root *Node) int {
current := root
for current.Left != nil {
current = current.Left
}
return current.Value
}
medium
A. All of the above
B. It does not handle empty tree (nil root)
C. It returns the minimum element instead of maximum
D. It incorrectly traverses left children instead of right
Solution
Step 1: Analyze traversal direction
The code loops while current.Left != nil, moving left, which finds minimum, not maximum.
Step 2: Check for other issues
Function does not check if root is nil, which can cause runtime error.
Final Answer:
It incorrectly traverses left children instead of right; does not handle nil root; returns minimum instead of maximum -> Option A
Quick Check:
Left traversal + no nil check = bugs [OK]
Hint: Traverse right, check nil root to avoid bugs [OK]
Common Mistakes:
Using left instead of right pointer
Ignoring nil root check
Assuming code finds maximum correctly
5. Given a BST where some nodes have duplicate values, how would you modify the Go function to find the maximum element considering duplicates are allowed on the right subtree?
Choose the best approach.
hard
A. Traverse left while current.Left != nil and current.Left.Value > current.Value
B. Return root.Value directly since duplicates are on right
C. Traverse right while current.Right != nil and current.Right.Value > current.Value
D. Traverse right while current.Right != nil only
Solution
Step 1: Understand duplicates in BST
In a BST allowing duplicates on right, right subtree values are >= current value.
Step 2: Best traversal for maximum
Traverse right while current.Right != nil only. Value checks like in C (>) stop early on equal duplicates before larger values (e.g., 10->10->12).
Final Answer:
Traverse right while current.Right != nil only -> Option D
Quick Check:
Structural rightmost traversal = max even with duplicates [OK]
Hint: Traverse right until nil regardless of duplicates [OK]
Common Mistakes:
Using strict > comparison, stopping early on equals
Traversing left instead of right
Returning root value without traversal
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.