Bird
Raised Fist0
DSA Javaprogramming~20 mins

BST Search Operation in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
BST Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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);
    }
}
ACompilation error
Bfalse
Ctrue
DRuntime exception
Attempts:
2 left
💡 Hint
Think about whether the value 7 exists in the BST.
❓ Predict Output
intermediate
2:00remaining
Output 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);
    }
}
Atrue
Bfalse
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Check if 8 is present in the BST nodes.
🧠 Conceptual
advanced
1:30remaining
Understanding BST Search Path
In a BST, when searching for the value 12 starting from the root node 10, which path will the search follow?
A10 -> 15
B10 -> 5 -> 7
C10 -> 15 -> 12
D10 -> 5 -> 3
Attempts:
2 left
💡 Hint
Remember BST property: left child < parent < right child.
🔧 Debug
advanced
2:30remaining
Identify 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);
}
AThe recursive calls for left and right subtrees are swapped.
BThe base case is missing to stop recursion.
CThe method does not return any value.
DThe comparison uses '==' instead of '.equals()'.
Attempts:
2 left
💡 Hint
Check which subtree to search when key is greater or smaller than root data.
🚀 Application
expert
3:00remaining
Number 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?
A5
B4
C2
D3
Attempts:
2 left
💡 Hint
Count each node visited including the root until the value is found.

Practice

(1/5)
1. What is the main purpose of the search operation in a Binary Search Tree (BST)?
easy
A. To insert a new value into the BST
B. To find whether a given value exists in the BST
C. To delete a value from the BST
D. To traverse all nodes in the BST

Solution

  1. Step 1: Understand the search operation

    The search operation checks if a specific value is present in the BST by comparing it with nodes.
  2. Step 2: Differentiate from other operations

    Insertion adds new nodes, deletion removes nodes, and traversal visits all nodes. Search only checks presence.
  3. Final Answer:

    To find whether a given value exists in the BST -> Option B
  4. Quick Check:

    Search = Find value presence [OK]
Hint: Search checks presence, not insertion or deletion [OK]
Common Mistakes:
  • Confusing search with insert or delete
  • Thinking search returns all nodes
  • Assuming search modifies the tree
2. Which of the following is the correct Java method signature for a BST search function that returns a boolean indicating if a value exists?
easy
A. public boolean search(int value)
B. public void search(int value)
C. public int search(int value)
D. public String search(int value)

Solution

  1. Step 1: Identify return type for search

    Search should return true or false, so return type must be boolean.
  2. Step 2: Check method signature correctness

    Method takes an int value to search and returns boolean. public boolean search(int value) matches this.
  3. Final Answer:

    public boolean search(int value) -> Option A
  4. Quick Check:

    Return type boolean for search [OK]
Hint: Search returns true/false, so use boolean return type [OK]
Common Mistakes:
  • Using void return type for search
  • Returning int or String instead of boolean
  • Incorrect method parameter types
3. Consider the following BST and the search code snippet:
class Node {
  int data;
  Node left, right;
  Node(int d) { data = d; left = right = null; }
}

public 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);
}

// BST structure:
//       10
//      /  \
//     5    15
//    / \     \
//   3   7     20

What is the output of search(root, 7)?
medium
A. null
B. false
C. true
D. Runtime error

Solution

  1. Step 1: Trace search for key 7 starting at root 10

    7 < 10, so search left subtree (node 5).
  2. Step 2: Trace search at node 5

    7 > 5, so search right subtree (node 7).
  3. Step 3: Trace search at node 7

    Node data equals key (7), so return true.
  4. Final Answer:

    true -> Option C
  5. Quick Check:

    7 found in BST = true [OK]
Hint: Follow BST rules left/right to find key [OK]
Common Mistakes:
  • Stopping search too early
  • Confusing left and right subtree directions
  • Assuming search returns node instead of boolean
4. The following Java method is intended to search a BST for a key. What is the bug?
public 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);
}
medium
A. The recursive calls for left and right subtrees are swapped
B. The base case is missing
C. The method should return int instead of boolean
D. The method does not handle null keys

Solution

  1. Step 1: Analyze recursive calls for BST search

    For BST, if key < root.data, search left; if key > root.data, search right.
  2. Step 2: Identify swapped calls

    Code calls search(root.left, key) when key > root.data, which is incorrect; left and right calls are swapped.
  3. Final Answer:

    The recursive calls for left and right subtrees are swapped -> Option A
  4. Quick Check:

    Left/right calls must match key comparison [OK]
Hint: Left subtree for smaller keys, right for larger keys [OK]
Common Mistakes:
  • Swapping left and right recursive calls
  • Missing base case for null root
  • Returning wrong data type
5. Given a BST with nodes inserted in this order: 15, 10, 20, 8, 12, 17, 25. Which sequence of nodes will the search operation visit when searching for 12?
hard
A. 10 -> 15 -> 12
B. 15 -> 20 -> 17 -> 12
C. 15 -> 10 -> 8 -> 12
D. 15 -> 10 -> 12

Solution

  1. Step 1: Build BST structure from insertion order

    Insert 15 as root; 10 left of 15; 20 right of 15; 8 left of 10; 12 right of 10; 17 left of 20; 25 right of 20.
  2. Step 2: Trace search path for key 12

    Start at 15: 12 < 15, go left to 10; 12 > 10, go right to 12; found key.
  3. Final Answer:

    15 -> 10 -> 12 -> Option D
  4. Quick Check:

    Search path follows BST rules [OK]
Hint: Follow left if smaller, right if larger until found [OK]
Common Mistakes:
  • Visiting nodes not on search path
  • Confusing insertion order with search path
  • Assuming search visits all nodes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard