Challenge - 5 Problems
Balanced BST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Balanced BST Inorder Traversal
Given a sorted array, a balanced binary search tree (BST) is created by picking the middle element as root and recursively doing the same for left and right subarrays. What is the inorder traversal output of the BST created from the array [1, 2, 3, 4, 5]?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def sorted_array_to_bst(arr): if not arr: return None mid = len(arr) // 2 root = Node(arr[mid]) root.left = sorted_array_to_bst(arr[:mid]) root.right = sorted_array_to_bst(arr[mid+1:]) return root def inorder(root): return inorder(root.left) + [root.val] + inorder(root.right) if root else [] root = sorted_array_to_bst([1, 2, 3, 4, 5]) print(inorder(root))
Attempts:
2 left
💡 Hint
Inorder traversal of a BST always returns the sorted order of elements.
✗ Incorrect
The inorder traversal of a BST visits nodes in ascending order. Since the BST is built from a sorted array, the inorder traversal returns the original sorted array.
🧠 Conceptual
intermediateHeight of Balanced BST from Sorted Array
If you create a balanced BST from a sorted array of length 15 by always choosing the middle element as root, what is the height of the resulting BST? (Height is the number of edges on the longest path from root to a leaf.)
Attempts:
2 left
💡 Hint
A balanced BST from n nodes has height about log2(n).
✗ Incorrect
For 15 nodes, height is floor(log2(15)) = 3 edges (4 levels).
🔧 Debug
advancedIdentify the Bug in BST Construction Code
What error will this code produce when converting a sorted array to a balanced BST?
```python
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def sorted_array_to_bst(arr):
if len(arr) == 0:
return None
mid = len(arr) // 2
root = Node(arr[mid])
root.left = sorted_array_to_bst(arr[mid+1:])
root.right = sorted_array_to_bst(arr[:mid])
return root
root = sorted_array_to_bst([1, 2, 3])
print(root.val)
```
Attempts:
2 left
💡 Hint
Check the slices used for left and right subtrees.
✗ Incorrect
The code swaps left and right subtree slices, so the tree is built incorrectly but no runtime error occurs.
❓ Predict Output
advancedOutput of Level Order Traversal of Balanced BST
What is the output of the level order traversal (breadth-first) of the balanced BST created from the sorted array [10, 20, 30, 40, 50, 60, 70]?
DSA Python
from collections import deque class Node: def __init__(self, val): self.val = val self.left = None self.right = None def sorted_array_to_bst(arr): if not arr: return None mid = len(arr) // 2 root = Node(arr[mid]) root.left = sorted_array_to_bst(arr[:mid]) root.right = sorted_array_to_bst(arr[mid+1:]) return root def level_order(root): if not root: return [] queue = deque([root]) result = [] while queue: node = queue.popleft() result.append(node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) return result root = sorted_array_to_bst([10, 20, 30, 40, 50, 60, 70]) print(level_order(root))
Attempts:
2 left
💡 Hint
Level order visits nodes level by level from left to right.
✗ Incorrect
The middle element 40 is root, left subtree root 20, right subtree root 60, and so on, producing the level order [40, 20, 60, 10, 30, 50, 70].
🧠 Conceptual
expertMinimum Number of Nodes for Given Height in Balanced BST
What is the minimum number of nodes in a balanced binary search tree of height 4 (edges)?
Attempts:
2 left
💡 Hint
A balanced BST of height h has at least 2^h nodes.
✗ Incorrect
Height 4 means 5 levels (0 to 4). Minimum nodes in a balanced BST is 2^(height+1) - 1 = 2^5 - 1 = 31.
