Smallest Gap Between Any Two Follower Counts Stored in a BST
Last updated: November 29, 2026
Quick Overview
A BST stores distinct creator follower counts. Return the minimum difference between any two values in the tree.
Meta
November 29, 20260
0
0 solved
A BST stores distinct creator follower counts. Return the minimum difference between any two values in the tree.
A quick Meta phone-screen BST question. The point is whether you realize an in-order traversal visits values in sorted order, so the answer is the smallest gap between consecutive visited values, with no need to compare every pair.
What the Interviewer Expects
- Use in-order traversal to get ascending order
- Compare each value only with its predecessor
- Avoid collecting all values into a list if possible
- Write the iterative traversal correctly
- State O(n) time and O(h) space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if the tree is a plain binary tree, not a BST?
- Return the pair of creators, not just the gap.
- Answer this after many inserts without a full traversal each time.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: root of a BST with at least two nodes and distinct integer values.
- Output: smallest
|a - b|over all pairs of nodes....
High-Level Design
In a sorted list, the closest pair is always adjacent: if a < b < c then c - a is larger than both b - a and c - b. In-order traversal of a BST produces exactly that sorted order.
So walk the...