Find the Kth Smallest Account ID in a BST Index

Last updated: November 29, 2026

Quick Overview

Given the root of a binary search tree of account IDs and an integer k, return the kth smallest ID (1-indexed).

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given the root of a binary search tree of account IDs and an integer k, return the kth smallest ID (1-indexed).

A short Meta phone screen problem that checks you exploit BST ordering and stop early. The follow-up about frequent inserts and queries probes whether you know about augmenting nodes with subtree sizes.

What the Interviewer Expects
  • Use inorder traversal for sorted order
  • Stop as soon as the kth node is popped
  • Iterate with an explicit stack
  • State O(h + k) time
  • Describe subtree-size augmentation for repeated queries
Key Topics to Cover
Binary search tree
Inorder traversal
Early termination
Order statistics
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
  • How would you support inserts and kth queries in O(h) each?
  • How would you find the kth largest?
  • What if the tree is modified between many queries?
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 is a BST root and 1 <= k <= n. Return the kth smallest value. The tree holds unique IDs....

High-Level Design

An inorder traversal of a BST yields values in increasing order, so the kth node popped is the answer. With an explicit stack, push the left spine, pop a node, count it, then move to its right child a...


Submit Your Answer
Markdown supported