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, then 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]?
What is the inorder traversal output of the BST created from the array [1, 2, 3, 4, 5]?
DSA Rust
fn inorder_traversal(root: &Option<Box<TreeNode>>) -> Vec<i32> {
match root {
None => vec![],
Some(node) => {
let mut left = inorder_traversal(&node.left);
left.push(node.val);
left.extend(inorder_traversal(&node.right));
left
}
}
}
// TreeNode struct
struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>,
right: Option<Box<TreeNode>>,
}
// Assume balanced BST is built correctly from [1,2,3,4,5]
// What does inorder_traversal(root) return?Attempts:
2 left
💡 Hint
Inorder traversal of a BST always returns the sorted order of elements.
✗ Incorrect
Inorder traversal visits left subtree, root, then right subtree. For a BST, this returns elements in ascending order. Since the tree is balanced but BST property holds, the output is the sorted array itself.
🧠 Conceptual
intermediateHeight of Balanced BST from Sorted Array
If you build a balanced binary search tree (BST) from a sorted array of length n, what is the height of the resulting BST in terms of n?
Attempts:
2 left
💡 Hint
Balanced BST splits array in half at each level.
✗ Incorrect
Each level of the tree splits the array roughly in half, so the height grows logarithmically with the number of elements.
🔧 Debug
advancedIdentify the Bug in BST Construction Code
This Rust function is intended to build a balanced BST from a sorted array. What error will it cause when run?
DSA Rust
fn build_bst(nums: &[i32]) -> Option<Box<TreeNode>> {
if nums.is_empty() {
return None;
}
let mid = nums.len() / 2;
Some(Box::new(TreeNode {
val: nums[mid],
left: build_bst(&nums[..mid]),
right: build_bst(&nums[mid+1..]),
}))
}Attempts:
2 left
💡 Hint
Check the slice passed to the right subtree recursive call.
✗ Incorrect
The right subtree call uses nums[mid..], which includes nums[mid] again, causing infinite recursion on the same slice.
📝 Syntax
advancedCorrect Rust Syntax for TreeNode Definition
Which option correctly defines a TreeNode struct in Rust for a binary tree node with integer value and optional left and right children?
Attempts:
2 left
💡 Hint
Children must be optional and boxed to allow recursive types.
✗ Incorrect
Option A correctly uses Option> to allow nullable children and boxing for recursive type size.
🚀 Application
expertNumber of Nodes at Each Level in Balanced BST
You build a balanced BST from a sorted array of length 15. How many nodes are there at level 3 (root is level 1)?
Attempts:
2 left
💡 Hint
Each level doubles the number of nodes from the previous level in a full binary tree.
✗ Incorrect
Level 1 has 1 node, level 2 has 2 nodes, level 3 has 4 nodes in a balanced binary tree.
