Dynamic Order-Statistic Binary Search Tree
Updated
Problem statement
Maintain an initially empty binary search tree of unique integer keys. Process each operation in order:
[1, value]insertsvalue. Inserting an existing key is a no-op.[2, value]deletesvalue. Deleting a missing key is a no-op.[3, k]asks for the one-indexedkth-smallest current key.
Every query rank is valid for the current tree. Return one integer for each query, in query order.
Store each node's subtree size so a query descends by comparing k with the left-subtree size.
Function
dynamicKthSmallest(operations: int[][]) → int[]Examples
Example 1
operations = [[1,5],[1,3],[1,7],[3,2],[2,5],[3,2]]return = [5,7]The first query sees [3,5,7]. After deleting 5, the second-smallest key is 7.
Example 2
operations = [[1,4],[1,4],[2,9],[3,1],[2,4],[1,2],[3,1]]return = [4,2]The duplicate insertion and missing deletion are no-ops. The two valid queries return 4 and 2.
Constraints
1 <= operations.length <= 100000.- Every operation has one of the three documented forms.
- All keys are signed
32-bit integers. - Every query rank is between
1and the current number of keys.
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
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Nested Todo ListNotion · PHONE SCREEN · ONSITE INTERVIEWNotion interview questions
- In-Memory File System With Directory SizesCapital One · PHONE SCREEN · ONSITE INTERVIEWCapital One interview questions
- Workspace File System With Folders and TablesClay · PHONE SCREEN
- Distributed Tree Counting State MachineSnowflake · PHONE SCREENSnowflake interview questions
- Office Employee HierarchyBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Colored Rooted TreeRubrik · ONSITE INTERVIEWRubrik interview questions
- Serialize and Deserialize a Binary TreeAmazon · ONSITE INTERVIEW
- Schedule Multiple Software TimersGoogle · ONSITE INTERVIEWGoogle interview questions