Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

BST Insert Operation in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
BST Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after inserting nodes in BST
What is the printed state of the BST after inserting the values 10, 5, 15, 3, 7 in that order? The BST is printed using in-order traversal (left -> root -> right).
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;
      }
    }
  }

  inOrder(node: Node | null, result: number[] = []) {
    if (!node) return result;
    this.inOrder(node.left, result);
    result.push(node.value);
    this.inOrder(node.right, result);
    return result;
  }
}

const bst = new BST();
bst.insert(10);
bst.insert(5);
bst.insert(15);
bst.insert(3);
bst.insert(7);
console.log(bst.inOrder(bst.root));
A[10, 5, 15, 3, 7]
B[3, 5, 7, 10, 15]
C[15, 10, 7, 5, 3]
D[3, 7, 5, 10, 15]
Attempts:
2 left
💡 Hint
Remember that in-order traversal of a BST prints values in sorted order.
❓ Predict Output
intermediate
2:00remaining
BST structure after inserting duplicate value
What is the in-order traversal output after inserting values 20, 10, 30, 20 into an empty BST? Assume duplicates are inserted to the right subtree.
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;
      }
    }
  }

  inOrder(node: Node | null, result: number[] = []) {
    if (!node) return result;
    this.inOrder(node.left, result);
    result.push(node.value);
    this.inOrder(node.right, result);
    return result;
  }
}

const bst = new BST();
bst.insert(20);
bst.insert(10);
bst.insert(30);
bst.insert(20);
console.log(bst.inOrder(bst.root));
A[10, 20, 20, 30]
B[10, 20, 30]
C[20, 10, 20, 30]
D[10, 30, 20, 20]
Attempts:
2 left
💡 Hint
Duplicates go to the right subtree, so the second 20 is placed as right child of the first 20.
🔧 Debug
advanced
2:00remaining
Identify the bug in BST insert method
What error will occur when running this BST insert code if we try to insert 5 into the BST with root 10 and left child 5?
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 (value > current.value) {
        if (!current.right) {
          current.right = newNode;
          return;
        }
        current = current.right;
      }
    }
  }
}

const bst = new BST();
bst.root = new Node(10);
bst.root.left = new Node(5);
bst.insert(5);
ANo error, insertion succeeds
BTypeError: Cannot read property 'left' of null
CSyntaxError: Unexpected token 'else if'
DInfinite loop causing program to hang
Attempts:
2 left
💡 Hint
Check what happens when value equals current.value in the loop.
❓ Predict Output
advanced
2:00remaining
BST structure after multiple inserts and in-order traversal
After inserting the values 50, 30, 70, 20, 40, 60, 80 into an empty BST, what is the in-order traversal output?
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;
      }
    }
  }

  inOrder(node: Node | null, result: number[] = []) {
    if (!node) return result;
    this.inOrder(node.left, result);
    result.push(node.value);
    this.inOrder(node.right, result);
    return result;
  }
}

const bst = new BST();
[50, 30, 70, 20, 40, 60, 80].forEach(v => bst.insert(v));
console.log(bst.inOrder(bst.root));
A[80, 70, 60, 50, 40, 30, 20]
B[50, 30, 20, 40, 70, 60, 80]
C[20, 30, 40, 50, 60, 70, 80]
D[20, 40, 30, 60, 80, 70, 50]
Attempts:
2 left
💡 Hint
In-order traversal prints BST values in ascending order.
🧠 Conceptual
expert
2:00remaining
Number of nodes in BST after insertions
If you insert the values [15, 10, 20, 10, 25, 15] into an empty BST where duplicates are inserted to the right subtree, how many nodes will the BST contain?
A6
B4
C3
D5
Attempts:
2 left
💡 Hint
Count each inserted value as a node, including duplicates inserted to the right.

Practice

(1/5)
1. What is the main purpose of the insert operation in a Binary Search Tree (BST)?
easy
A. To add a new node while keeping the BST property intact
B. To delete a node from the BST
C. To find the maximum value in the BST
D. To balance the BST after insertion

Solution

  1. Step 1: Understand BST property

    A BST keeps nodes ordered so that left child is smaller and right child is larger than the parent.
  2. Step 2: Purpose of insert operation

    Insert adds a new node in the correct position to maintain this order.
  3. Final Answer:

    To add a new node while keeping the BST property intact -> Option A
  4. Quick Check:

    Insert = Add node maintaining order [OK]
Hint: Insert adds nodes without breaking BST order [OK]
Common Mistakes:
  • Confusing insert with delete operation
  • Thinking insert balances the tree automatically
  • Assuming insert finds max or min values
2. Which of the following is the correct TypeScript function signature for inserting a value into a BST node?
easy
A. function insert(root: Node, value: number): void
B. function insert(value: number, root: Node): void
C. function insert(root: Node | null, value: number): Node | null
D. function insert(root: Node, value: string): Node

Solution

  1. Step 1: Analyze parameter types

    Insert needs the root node (which can be null if tree is empty) and the value to insert (a number).
  2. Step 2: Analyze return type

    It returns the updated root node (Node or null if empty).
  3. Final Answer:

    function insert(root: Node | null, value: number): Node | null -> Option C
  4. Quick Check:

    Correct params and return type [OK]
Hint: Insert returns updated root, accepts root or null [OK]
Common Mistakes:
  • Swapping parameter order
  • Using void return type instead of returning Node
  • Using wrong value type like string
3. Given the following BST and the insert operation call insert(root, 7), what will be the in-order traversal of the BST after insertion?

Initial BST in-order: 3 -> 5 -> 8 -> 10

Code snippet:
class Node {
  constructor(public val: number, public left: Node | null = null, public right: Node | null = null) {}
}

function insert(root: Node | null, val: number): Node {
  if (!root) return new Node(val);
  if (val < root.val) root.left = insert(root.left, val);
  else root.right = insert(root.right, val);
  return root;
}
medium
A. [3, 5, 8, 7, 10]
B. [3, 5, 8, 10, 7]
C. [7, 3, 5, 8, 10]
D. [3, 5, 7, 8, 10]

Solution

  1. Step 1: Locate insertion point for 7

    7 is greater than 5 but less than 8, so it goes to the left of 8.
  2. Step 2: Perform in-order traversal after insertion

    In-order visits left, root, right: 3, 5, 7, 8, 10.
  3. Final Answer:

    [3, 5, 7, 8, 10] -> Option D
  4. Quick Check:

    In-order sorted after insert [OK]
Hint: Insert left if smaller, right if larger [OK]
Common Mistakes:
  • Placing 7 incorrectly after 8
  • Confusing in-order traversal order
  • Ignoring recursive insert logic
4. The following TypeScript code attempts to insert a value into a BST but has a bug. What is the error?

function insert(root: Node | null, val: number): Node {
  if (root === null) {
    return new Node(val);
  }
  if (val <= root.val) {
    insert(root.left, val);
  } else {
    insert(root.right, val);
  }
  return root;
}
medium
A. The base case is missing
B. The recursive calls do not update root.left or root.right
C. The function returns void instead of Node
D. The comparison operator should be < instead of <=

Solution

  1. Step 1: Check recursive calls

    The calls to insert do not assign the result back to root.left or root.right, so tree structure is not updated.
  2. Step 2: Correct recursive assignment

    Should be root.left = insert(root.left, val) or root.right = insert(root.right, val) to update links.
  3. Final Answer:

    The recursive calls do not update root.left or root.right -> Option B
  4. Quick Check:

    Must assign recursive insert result to child [OK]
Hint: Always assign recursive insert result to child node [OK]
Common Mistakes:
  • Forgetting to assign recursive call result
  • Misunderstanding base case presence
  • Changing comparison operator unnecessarily
5. You have a BST with nodes 10, 5, 15, 3, 7, 12, 18. You want to insert 6. Which sequence of nodes will you visit during the insert operation?
hard
A. [10, 5, 7]
B. [10, 15, 12]
C. [10, 5, 3]
D. [10, 15, 18]

Solution

  1. Step 1: Start at root 10

    6 is less than 10, so move to left child 5.
  2. Step 2: Compare with 5 and 7

    6 is greater than 5, move to right child 7. 6 is less than 7, so insert here.
  3. Final Answer:

    [10, 5, 7] -> Option A
  4. Quick Check:

    Traverse nodes comparing 6 until insertion point [OK]
Hint: Follow left/right based on comparison until null [OK]
Common Mistakes:
  • Visiting wrong subtree based on comparison
  • Skipping nodes in path
  • Confusing insertion path with traversal order
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