What is a binary search tree (BST)?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Data Structures & Algorithms development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
A Binary Search Tree (BST) is a binary tree with the BST property: for every node, all values in the left subtree are less than the node's value, and all values in the right subtree are greater. This property enables efficient search. Operations: Search: compare target with current node, go left (smaller) or right (larger), O(h) where h = height; Insert: search for the position, insert as a leaf, O(h); Delete: three cases — leaf (just remove), one child (replace with child), two children (replace with in-order successor/predecessor), O(h); In-order traversal: visits nodes in sorted order (left, root, right). Average case h = O(log n) for random data. Worst case h = O(n) for sorted input (degenerates to linked list). Balanced BSTs: AVL trees and Red-Black trees maintain O(log n) height through rotations. Java TreeMap, C++ std::map use Red-Black trees. Time: O(log n) average, O(n) worst for search/insert/delete. In-order traversal produces sorted output in O(n). BSTs are used in databases (indexed columns), file systems, and anywhere sorted order + fast search is needed.
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.