Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

BST Search Operation in DSA Typescript - 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 will be the output of the following TypeScript code that searches for a value in a Binary Search Tree?
DSA Typescript
class Node {
  value: number;
  left: Node | null = null;
  right: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class BST {
  root: Node | null = null;

  insert(value: number) {
    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: number): boolean {
    let current = this.root;
    while (current) {
      if (value === current.value) return true;
      if (value < current.value) current = current.left;
      else current = current.right;
    }
    return false;
  }
}

const tree = new BST();
tree.insert(10);
tree.insert(5);
tree.insert(15);
tree.insert(3);
tree.insert(7);

console.log(tree.search(7));
Afalse
Btrue
Cundefined
DTypeError
Attempts:
2 left
💡 Hint
Think about whether the value 7 exists in the tree after insertion.
❓ Predict Output
intermediate
2:00remaining
Output of BST Search for Non-Existing Value
What will be the output of the following TypeScript code that searches for a value not present in the Binary Search Tree?
DSA Typescript
class Node {
  value: number;
  left: Node | null = null;
  right: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class BST {
  root: Node | null = null;

  insert(value: number) {
    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: number): boolean {
    let current = this.root;
    while (current) {
      if (value === current.value) return true;
      if (value < current.value) current = current.left;
      else current = current.right;
    }
    return false;
  }
}

const tree = new BST();
tree.insert(10);
tree.insert(5);
tree.insert(15);
tree.insert(3);
tree.insert(7);

console.log(tree.search(8));
Aundefined
Btrue
Cfalse
DReferenceError
Attempts:
2 left
💡 Hint
Check if 8 was inserted into the tree before searching.
🧠 Conceptual
advanced
2:00remaining
Understanding BST Search Path
In a Binary Search Tree, when searching for the value 12, which path will the search follow given the tree below? Tree structure: - Root: 10 - Left child of 10: 5 - Right child of 10: 15 - Left child of 15: 12 - Right child of 15: 20 Choose the correct sequence of nodes visited during the search for 12.
A10 -> 12
B10 -> 5 -> 12
C10 -> 15 -> 20
D10 -> 15 -> 12
Attempts:
2 left
💡 Hint
Remember that in BST, values less than current node go left, greater go right.
❓ Predict Output
advanced
2:00remaining
Output of BST Search with Null Root
What will be the output of the following TypeScript code when searching in an empty Binary Search Tree?
DSA Typescript
class Node {
  value: number;
  left: Node | null = null;
  right: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class BST {
  root: Node | null = null;

  search(value: number): boolean {
    let current = this.root;
    while (current) {
      if (value === current.value) return true;
      if (value < current.value) current = current.left;
      else current = current.right;
    }
    return false;
  }
}

const tree = new BST();
console.log(tree.search(5));
Afalse
BTypeError
Ctrue
Dundefined
Attempts:
2 left
💡 Hint
The tree is empty, so no nodes exist to find any value.
❓ Predict Output
expert
3:00remaining
Output of BST Search After Multiple Insertions and Searches
Consider the following TypeScript code that inserts multiple values into a BST and then searches for several values. What will be the output printed to the console?
DSA Typescript
class Node {
  value: number;
  left: Node | null = null;
  right: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class BST {
  root: Node | null = null;

  insert(value: number) {
    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: number): boolean {
    let current = this.root;
    while (current) {
      if (value === current.value) return true;
      if (value < current.value) current = current.left;
      else current = current.right;
    }
    return false;
  }
}

const tree = new BST();
tree.insert(20);
tree.insert(10);
tree.insert(30);
tree.insert(25);
tree.insert(35);
tree.insert(5);
tree.insert(15);

console.log(tree.search(15));
console.log(tree.search(40));
console.log(tree.search(5));
console.log(tree.search(17));
A
true
false
true
false
B
false
false
true
false
C
true
true
true
false
D
true
false
false
true
Attempts:
2 left
💡 Hint
Check each searched value against the inserted values carefully.

Practice

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

Solution

  1. Step 1: Understand the purpose of search in BST

    The search operation is designed to check if a specific value is present in the BST.
  2. Step 2: Differentiate from other operations

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

    To find whether a given value exists in the BST -> Option C
  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 TypeScript function signature for a BST search method that returns a boolean?
easy
A. function search(root: TreeNode | null, value: number): boolean
B. function search(value: number): TreeNode
C. function search(root: TreeNode, value: string): boolean
D. function search(root: TreeNode | null, value: number): void

Solution

  1. Step 1: Analyze parameter types

    The search needs the root node (which can be null) and the value to find (a number).
  2. Step 2: Analyze return type

    Search returns true or false, so return type should be boolean.
  3. Final Answer:

    function search(root: TreeNode | null, value: number): boolean -> Option A
  4. Quick Check:

    Correct params and boolean return [OK]
Hint: Search returns boolean, root can be null [OK]
Common Mistakes:
  • Using wrong parameter types
  • Returning void instead of boolean
  • Using string instead of number for value
3. Given the BST below and the search function call search(root, 7), what will be the output?

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

Code snippet:
function search(root: TreeNode | null, value: number): boolean {
  if (!root) return false;
  if (root.val === value) return true;
  if (value < root.val) return search(root.left, value);
  return search(root.right, value);
}

console.log(search(root, 7));
medium
A. false
B. Error
C. undefined
D. true

Solution

  1. Step 1: Trace search for value 7

    Start at root 10: 7 < 10, go left to 5. Then 7 > 5, go right to 7.
  2. Step 2: Check node value

    Node value is 7, matches search value, so return true.
  3. Final Answer:

    true -> Option D
  4. Quick Check:

    Found 7 in BST = true [OK]
Hint: Follow BST rules left/right to find value [OK]
Common Mistakes:
  • Stopping search too early
  • Confusing left and right child directions
  • Returning false when node matches
4. The following TypeScript search function for a BST has a bug. What is the error?

function search(root: TreeNode | null, value: number): boolean {
  if (root === null) return false;
  if (root.val === value) return true;
  if (value > root.val) return search(root.left, value);
  else return search(root.right, value);
}
medium
A. The comparison operator should be >= instead of >
B. The recursive calls to left and right children are swapped
C. The function does not return a boolean
D. The base case is missing

Solution

  1. Step 1: Check recursive calls logic

    For BST, if value > root.val, search should go right, but code goes left.
  2. Step 2: Confirm swapped children calls

    Similarly, else goes right instead of left. So left and right calls are swapped.
  3. Final Answer:

    The recursive calls to left and right children are swapped -> Option B
  4. Quick Check:

    Left/right calls swapped = bug [OK]
Hint: Value > root goes right, not left [OK]
Common Mistakes:
  • Mixing left and right child in recursion
  • Ignoring base case (though present here)
  • Wrong comparison operators
5. You want to search for a value in a BST that may contain duplicate values on the right subtree. Which modification to the standard BST search logic ensures you find the first occurrence of the value?

Choose the correct approach:
hard
A. Always search left subtree when value equals current node's value
B. Return true immediately when value equals current node's value
C. Search right subtree when value equals current node's value to find duplicates
D. Traverse entire tree inorder to find all occurrences

Solution

  1. Step 1: Understand duplicate placement in BST

    Duplicates are stored in right subtree, so first occurrence is the leftmost matching node.
  2. Step 2: Modify search to find first occurrence

    When value equals current node, continue searching left subtree to find earlier occurrence.
  3. Final Answer:

    Always search left subtree when value equals current node's value -> Option A
  4. Quick Check:

    Search left on equal to find first duplicate [OK]
Hint: On equal, go left to find first duplicate [OK]
Common Mistakes:
  • Stopping search immediately on equal
  • Searching right subtree on equal
  • Doing full inorder traversal unnecessarily
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