0
0
DSA Javascriptprogramming~20 mins

BST Property and Why It Matters in DSA Javascript - Practice Problems & Challenges

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

  inOrder(node, result = []) {
    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).join(' -> ') + ' -> null');
A15 -> 10 -> 7 -> 5 -> 3 -> null
B3 -> 5 -> 7 -> 10 -> 15 -> null
C10 -> 5 -> 3 -> 7 -> 15 -> null
D5 -> 3 -> 7 -> 10 -> 15 -> null
Attempts:
2 left
💡 Hint
Remember, in-order traversal of a BST prints values in sorted order.
🧠 Conceptual
intermediate
1:30remaining
Why BST Property Matters for Search Efficiency
Why does the BST property (left child < parent < right child) make searching efficient compared to an unsorted binary tree?
AIt balances the tree automatically, so all paths have the same length.
BIt stores all values in a linked list, making search linear but simple.
CIt allows skipping half of the tree at each step, reducing search time to O(log n) on average.
DIt stores values randomly, so search time is unpredictable.
Attempts:
2 left
💡 Hint
Think about how the BST property helps decide which subtree to explore next.
🔧 Debug
advanced
2:00remaining
Identify the Bug in BST Insertion
What error will this code cause when inserting nodes into a BST?
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;
      }
    }
  }
}

const bst = new BST();
bst.insert(10);
bst.insert(10);
console.log(bst.root.left.value);
AIt prints null because the left child is never assigned.
BIt throws a TypeError because left is undefined.
CIt causes an infinite loop because duplicates are not handled properly.
DIt prints 10 because duplicates go to the left subtree.
Attempts:
2 left
💡 Hint
Check how the code handles values equal to current node's value.
Predict Output
advanced
2:30remaining
Output After Deleting a Node in BST
What is the in-order traversal output after deleting node with value 5 from this BST? Inserted nodes: 10, 5, 15, 3, 7. Then delete 5.
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;
      }
    }
  }

  findMin(node) {
    while (node.left) {
      node = node.left;
    }
    return node;
  }

  delete(value, node = this.root) {
    if (!node) return null;
    if (value < node.value) {
      node.left = this.delete(value, node.left);
    } else if (value > node.value) {
      node.right = this.delete(value, node.right);
    } else {
      if (!node.left) return node.right;
      if (!node.right) return node.left;
      let minRight = this.findMin(node.right);
      node.value = minRight.value;
      node.right = this.delete(minRight.value, node.right);
    }
    return node;
  }

  inOrder(node, result = []) {
    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);
bst.delete(5);
console.log(bst.inOrder(bst.root).join(' -> ') + ' -> null');
A3 -> 7 -> 10 -> 15 -> null
B3 -> 5 -> 7 -> 10 -> 15 -> null
C7 -> 10 -> 15 -> null
D3 -> 10 -> 15 -> null
Attempts:
2 left
💡 Hint
When deleting a node with two children, replace it with the smallest node in the right subtree.
🧠 Conceptual
expert
1:30remaining
Effect of Violating BST Property on Search
If a binary tree does not maintain the BST property, what is the worst-case time complexity of searching for a value in it?
AO(n), because without ordering, search may need to check every node.
BO(log n), because the tree structure still halves search space.
CO(1), because any node can be found instantly.
DO(n log n), because search requires sorting first.
Attempts:
2 left
💡 Hint
Think about how ordering helps reduce search steps.