Challenge - 5 Problems
BST Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of searching for 7 in this BST?
Consider the following JavaScript code that builds a simple Binary Search Tree (BST) and searches for the value 7. What will be the output printed?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } class BST { constructor() { this.root = null; } insert(value) { const newNode = new Node(value); if (!this.root) { this.root = newNode; return; } let current = this.root; while (true) { if (value < current.value) { if (!current.left) { current.left = newNode; return; } current = current.left; } else { if (!current.right) { current.right = newNode; return; } current = current.right; } } } search(value) { let current = this.root; while (current) { if (value === current.value) { return true; } else if (value < current.value) { current = current.left; } else { current = current.right; } } return false; } } const bst = new BST(); bst.insert(10); bst.insert(5); bst.insert(15); bst.insert(3); bst.insert(7); bst.insert(12); bst.insert(18); console.log(bst.search(7));
Attempts:
2 left
💡 Hint
Trace the path from root 10 to find 7 by comparing values.
✗ Incorrect
The search starts at the root (10). Since 7 is less than 10, it moves left to 5. Then 7 is greater than 5, so it moves right to 7. It finds the value and returns true.
❓ Predict Output
intermediate2:00remaining
What is the output when searching for 6 in this BST?
Given the same BST as before, what will be the output of searching for the value 6?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } class BST { constructor() { this.root = null; } insert(value) { const newNode = new Node(value); if (!this.root) { this.root = newNode; return; } let current = this.root; while (true) { if (value < current.value) { if (!current.left) { current.left = newNode; return; } current = current.left; } else { if (!current.right) { current.right = newNode; return; } current = current.right; } } } search(value) { let current = this.root; while (current) { if (value === current.value) { return true; } else if (value < current.value) { current = current.left; } else { current = current.right; } } return false; } } const bst = new BST(); bst.insert(10); bst.insert(5); bst.insert(15); bst.insert(3); bst.insert(7); bst.insert(12); bst.insert(18); console.log(bst.search(6));
Attempts:
2 left
💡 Hint
Check if 6 exists by moving left or right from root.
✗ Incorrect
The search moves from 10 to 5 (since 6 < 10), then from 5 to 7 (since 6 > 5). Since 7 has no left child, the search ends without finding 6, returning false.
🔧 Debug
advanced2:00remaining
What error does this BST search code produce?
Examine the following BST search method. What error will it cause when searching for any value?
DSA Javascript
search(value) {
let current = this.root;
while (current !== null) {
if (value = current.value) {
return true;
} else if (value < current.value) {
current = current.left;
} else {
current = current.right;
}
}
return false;
}Attempts:
2 left
💡 Hint
Look carefully at the if condition inside the while loop.
✗ Incorrect
The condition uses assignment (=) instead of comparison (===). This assigns current.value to value, which is truthy, so the if condition is always true, causing an infinite loop.
🧠 Conceptual
advanced2:00remaining
How many nodes are visited when searching for 12 in this BST?
Given the BST constructed by inserting values in this order: 10, 5, 15, 3, 7, 12, 18. How many nodes will the search method visit when searching for 12?
Attempts:
2 left
💡 Hint
Trace the path from root to 12 counting nodes visited.
✗ Incorrect
Search path: 10 (root) → 15 (right child) → 12 (left child of 15). Total nodes visited = 3.
❓ Predict Output
expert2:00remaining
What is the output of searching for 20 in this BST after these operations?
After inserting 10, 5, 15, 3, 7, 12, 18 into a BST, the following code runs. What is the output?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } class BST { constructor() { this.root = null; } insert(value) { const newNode = new Node(value); if (!this.root) { this.root = newNode; return; } let current = this.root; while (true) { if (value < current.value) { if (!current.left) { current.left = newNode; return; } current = current.left; } else { if (!current.right) { current.right = newNode; return; } current = current.right; } } } search(value) { let current = this.root; while (current) { if (value === current.value) { return true; } else if (value < current.value) { current = current.left; } else { current = current.right; } } return false; } } const bst = new BST(); bst.insert(10); bst.insert(5); bst.insert(15); bst.insert(3); bst.insert(7); bst.insert(12); bst.insert(18); console.log(bst.search(20));
Attempts:
2 left
💡 Hint
20 is greater than all inserted values; check rightmost path.
✗ Incorrect
Search path: 10 → 15 → 18. Since 20 > 18 and 18 has no right child, search ends returning false.