Challenge - 5 Problems
BST Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of BST Search for Existing Value
What is the output of the following Java code that searches for a value in a Binary Search Tree (BST)?
DSA Java
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } class BST { Node root; boolean search(Node root, int key) { if (root == null) return false; if (root.data == key) return true; if (key < root.data) return search(root.left, key); else return search(root.right, key); } } public class Main { public static void main(String[] args) { BST tree = new BST(); tree.root = new Node(10); tree.root.left = new Node(5); tree.root.right = new Node(15); tree.root.left.left = new Node(3); tree.root.left.right = new Node(7); boolean found = tree.search(tree.root, 7); System.out.println(found); } }
Attempts:
2 left
💡 Hint
Think about whether the value 7 exists in the BST.
✗ Incorrect
The search method recursively checks nodes. Since 7 is present in the BST, the method returns true and prints it.
❓ Predict Output
intermediateOutput of BST Search for Non-Existing Value
What is the output of the following Java code that searches for a value not present in the BST?
DSA Java
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } class BST { Node root; boolean search(Node root, int key) { if (root == null) return false; if (root.data == key) return true; if (key < root.data) return search(root.left, key); else return search(root.right, key); } } public class Main { public static void main(String[] args) { BST tree = new BST(); tree.root = new Node(10); tree.root.left = new Node(5); tree.root.right = new Node(15); tree.root.left.left = new Node(3); tree.root.left.right = new Node(7); boolean found = tree.search(tree.root, 8); System.out.println(found); } }
Attempts:
2 left
💡 Hint
Check if 8 is present in the BST nodes.
✗ Incorrect
The search method returns false because 8 is not found in the BST.
🧠 Conceptual
advancedUnderstanding BST Search Path
In a BST, when searching for the value 12 starting from the root node 10, which path will the search follow?
Attempts:
2 left
💡 Hint
Remember BST property: left child < parent < right child.
✗ Incorrect
Since 12 > 10, search moves right to 15. Then 12 < 15, so it would go left if 12 existed. But since 12 is not in the tree, search ends at 15.
🔧 Debug
advancedIdentify the Bug in BST Search Method
What is the bug in the following BST search method that causes incorrect search results?
DSA Java
boolean search(Node root, int key) { if (root == null) return false; if (root.data == key) return true; if (key > root.data) return search(root.left, key); else return search(root.right, key); }
Attempts:
2 left
💡 Hint
Check which subtree to search when key is greater or smaller than root data.
✗ Incorrect
In BST, if key > root.data, search should go right subtree, but code incorrectly goes left, causing wrong results.
🚀 Application
expertNumber of Recursive Calls in BST Search
Given a BST with nodes inserted in this order: 20, 10, 30, 5, 15, 25, 35. How many recursive calls will the search method make when searching for the value 15?
Attempts:
2 left
💡 Hint
Count each node visited including the root until the value is found.
✗ Incorrect
Search path: 20 (call 1) -> 10 (call 2) -> 15 (call 3). Found at third call.
