Binary Search Tree (BST)

Trees & Graphs

Ordered hierarchical storage with monotonic inorder traversal

38 problems·9 Easy·26 Medium·3 Hard
Pattern Study Guide & Cheat Sheet
▼

Exploit the BST invariant (every left descendant < root < every right descendant) to search, insert, and validate in O(height) time without inspecting the whole tree.

Core Invariant: BST Invariant: Validating a node requires verifying it against an ancestor range `(low, high)`, NOT just its immediate parent. Inorder traversal of a BST always yields strictly sorted ascending order.

Recognize it (Keywords & Signals)
  • Validate Binary Search Tree
  • Lowest Common Ancestor in BST
  • Kth smallest / largest element in BST
  • Convert sorted array / list to balanced BST
  • Delete / Insert node in BST
When NOT to use

Tree does not follow BST ordering property or duplicates are allowed without clear convention.

How to solve (Step-by-step)
  1. 1.For validation: pass valid bounds `(low, high)` down the tree: `low < node.val < high`.
  2. 2.For search/LCA: compare `p.val` and `q.val` with `curr.val`. If both smaller, go left; if both larger, go right; if they split, `curr` IS the LCA!
  3. 3.For kth smallest: execute iterative inorder traversal using stack; the k-th popped node is the k-th smallest.
Watch for (Interview Traps)
  • Only checking immediate parent: `node.left.val < node.val` allows invalid grand-ancestors (e.g. right child of left subtree > root)
  • Handling `<` vs `<=`: LeetCode standard BST has strictly distinct values (`low < val < high`)
  • Forgetting that BST operations degrade to O(N) if the tree is unbalanced
Validate BST (Range Invariant) & Inorder Traversal
def is_valid_bst(root: TreeNode | None) -> bool:
    def validate(node: TreeNode | None, low: float, high: float) -> bool:
        if not node:
            return True
        # Node value must strictly lie within inherited ancestor bounds
        if not (low < node.val < high):
            return False
            
        # Left children must be < node.val; Right children must be > node.val
        return (validate(node.left, low, node.val) and 
                validate(node.right, node.val, high))
                
    return validate(root, float('-inf'), float('inf'))
Cost
O(H) where H = height (O(log N) balanced, O(N) skewed) · O(H) stack depth (Inorder traversal yields sorted order in O(N) time.)
Canonical problems
#98 Validate Binary Search Tree: Verify ancestor bounds (low, high) on every node
#235 Lowest Common Ancestor of a BST: O(H) search: split point between p and q is the LCA
#230 Kth Smallest Element in a BST: Inorder traversal visits nodes in sorted order; stop at k
#108 Convert Sorted Array to Binary Search Tree: Pick middle element as root and recurse on halves
Medium·25 companies·Max freq 88%·Acc 0.4%
WixNeetCode 150Blind 75+22
Medium·16 companies·Max freq 100%·Acc 0.7%
XXNeetCode 150+13
Medium·15 companies·Max freq 88%·Acc 77.0%
UberNeetCode 150Blind 75+12
Easy·13 companies·Max freq 93%·Acc 61.3%
TinderAdobeNeetCode 150+10
Medium·12 companies·Max freq 88%·Acc 63.9%
SnapSnapClari+9
Medium·11 companies·Max freq 55%·Acc 55.0%
UberOracleByteDance+8
Medium·10 companies·Max freq 100%·Acc 66.9%
ZenefitsLyftSalesforce+7
Easy·9 companies·Max freq 88%·Acc 49.2%
SnapSnapFractal Analytics+6
Easy·9 companies·Max freq 75%·Acc 75.8%
AirbnbAccentureAmazon+6
Medium·9 companies·Max freq 63%·Acc 76.7%
MetaLinkedInWalmart Labs+6
Medium·9 companies·Max freq 53%·Acc 60.2%
Media.netMedia.netMicrosoft+6
Medium·7 companies·Max freq 63%·Acc 73.5%
AndurilMicrosoftAmazon+4
Medium·7 companies·Max freq 43%·Acc 84.4%
LyftAmazonSalesforce+4
Medium·6 companies·Max freq 100%·Acc 51.2%
Pocket GemsDocusignMicrosoft+3
Easy·6 companies·Max freq 78%·Acc 87.6%
MetaYandexGoogle+3
Easy·6 companies·Max freq 27%·Acc 82.9%
GoogletcsAmazon+3
Medium·5 companies·Max freq 100%·Acc 51.8%
ZenefitsExpediaSalesforce+2
Hard·5 companies·Max freq 38%·Acc 48.1%
AmazonGoogleMicrosoft+2
Medium·5 companies·Max freq 38%·Acc 86.3%
AmazonMetaMicrosoft+2
Medium·5 companies·Max freq 31%·Acc 62.8%
MetaAmazonGoogle+2
Easy·4 companies·Max freq 88%·Acc 61.6%
WixGoogleMeta+1
Medium·4 companies·Max freq 69%·Acc 59.8%
ShopeeAmazonMicrosoft+1
Medium·4 companies·Max freq 64%·Acc 65.6%
MetaTikTokAmazon+1
Hard·4 companies·Max freq 63%·Acc 61.2%
LinkedInGoogleUber+1
Medium·4 companies·Max freq 56%·Acc 88.4%
SAPAmazonApple+1
Medium·4 companies·Max freq 44%·Acc 66.8%
AdobeBloombergAmazon+1
Easy·4 companies·Max freq 26%·Acc 59.1%
GoogleAmazonMeta+1
Medium·4 companies·Max freq 25%·Acc 71.8%
AmazonMicrosoftGoogle+1
Medium·3 companies·Max freq 100%·Acc 82.2%
CoupangAmazonGoogle
Medium·3 companies·Max freq 84%·Acc 61.1%
Arista NetworksMetaGoogle
Easy·3 companies·Max freq 29%·Acc 59.5%
GoogleMetaAmazon
Hard·3 companies·Max freq 13%·Acc 39.6%
AmazonGoogleMicrosoft
Medium·2 companies·Max freq 28%·Acc 45.9%
MicrosoftMeta
Medium·2 companies·Max freq 25%·Acc 80.3%
AmazonMeta
Easy·2 companies·Max freq 13%·Acc 79.0%
GoogleAmazon
Medium·1 companies·Max freq 25%·Acc 63.5%
Meta
Medium·1 companies·Max freq 25%·Acc 68.2%
Amazon
Medium·1 companies·Max freq 25%·Acc 44.7%
Google
38 displayed (of 38 matching)Filtered: All Companies

Top Companies Testing Binary Search Tree (BST)

Browse company-specific interview problem lists and recency breakdowns:

Explore All 22 DSA Coding Patterns

Master core algorithmic patterns tested in FAANG & Big Tech coding interviews.

Patterns Hub

Binary Search Tree (BST) frequently asked questions

What is the Binary Search Tree (BST) pattern in coding interviews?

Ordered hierarchical storage with monotonic inorder traversal It is categorized under Trees & Graphs and features 38 curated interview problems across Easy (9), Medium (26), and Hard (3) difficulties.

Which tech companies frequently test Binary Search Tree (BST)?

Companies frequently asking Binary Search Tree (BST) questions include Amazon, Google, Meta, Microsoft, Bloomberg, NeetCode 150.

How do I master the Binary Search Tree (BST) pattern?

Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.