FastPrepKth Smallest Element in a Binary Search Tree
Problems/Kth Smallest Element in a Binary Search Tree

Kth Smallest Element in a Binary Search Tree

Salesforce logoSalesforce● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Salesforce onsite interview. The report gives a binary search tree directly. Here you get a list of unique keys in insertion order, and we build the same tree from it.

Insert the values in insertionOrder into an initially empty binary search tree in the given order. Then return the kth-smallest key, where k is one-indexed.

Use an iterative inorder traversal and stop as soon as the kth node is visited.

Function

kthSmallest(insertionOrder: int[], k: int) → int

Examples

Example 1

insertionOrder = [5,3,6,2,4,1]k = 3return = 3

The inorder sequence is [1,2,3,4,5,6], whose third value is 3.

Example 2

insertionOrder = [2,1,3]k = 1return = 1

The first node visited by inorder traversal has key 1.

Constraints

  • 1 <= insertionOrder.length <= 100000.
  • All values in insertionOrder are distinct signed 32-bit integers.
  • 1 <= k <= insertionOrder.length.

More Salesforce Onsite Interview questions

  • Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
  • Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
  • 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
  • Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
  • Group Identical Tree LevelsONSITE INTERVIEW · Seen Oct 2026
  • Meeting Rooms IIONSITE INTERVIEW · Seen Oct 2026
  • Construct a Tree from Level-Order and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
  • Maximize Monsters DefeatedONSITE INTERVIEW · Seen Sep 2026

Similar questions at other companies

  • Filesystem Entity Total SizeGoogle · ONSITE INTERVIEW
  • Render a Comment HierarchyAmazon · ONSITE INTERVIEW
  • Delete a Filesystem TreeDatadog · PHONE SCREEN · ONSITE INTERVIEW
  • Flatten and Reconstruct an Ordered TreeThe Walt Disney Company · ONSITE INTERVIEW
  • Inorder, Preorder and Postorder TraversalsAMD · ONSITE INTERVIEW
  • Compare N-Ary Tree Leaf SequencesNavan · ONSITE INTERVIEW
  • Flatten JSON into Repeated Key-Value PairsKlaviyo · ONSITE INTERVIEW
  • Kth Smallest Value in a BSTExpedia · PHONE SCREEN · ONSITE INTERVIEW
  • Validate Binary Search TreeInMobi · ONSITE INTERVIEW
  • Serialize a Binary Search TreeConfluent · ONSITE INTERVIEW
See Salesforce hiring insights
Saved
class Solution {
    public int kthSmallest(int[] insertionOrder, int k) {
        // TODO: build the BST and stop the inorder traversal at rank k.
    }
}
Checking account…