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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

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
BST
in-order traversal
iterative stack
sorted order
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported