Kth Shortest Trip Duration in an Order-Statistic BST
Last updated: November 29, 2026
Quick Overview
Implement a structure that supports insert(duration) and kth(k), returning the k-th smallest trip duration seen so far (1-indexed). Both operations should be fast when the tree stays balanced.
Uber
November 29, 20260
0
0 solved
Implement a structure that supports insert(duration) and kth(k), returning the k-th smallest trip duration seen so far (1-indexed). Both operations should be fast when the tree stays balanced.
Uber screens use this to check whether you can augment a BST with subtree sizes, the core idea behind order-statistic trees. It relates to percentile queries on trip durations in marketplace analytics.
What the Interviewer Expects
- Augments each node with the size of its subtree
- Navigates left, self or right using left subtree size
- Updates sizes during insert
- Handles duplicates by a tie rule or a count field
- Explains O(h) per operation and the need for balancing
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
- How would you support delete?
- How would you guarantee O(log n) worst case?
- How would you get the median of the last hour of trips only?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
insert(x): add a duration (duplicates allowed).kth(k): return the k-th smallest,1 <= k <= size.- Many interleaved operations, so avoid re-sorting....
High-Level Design
Store size on every node: the number of nodes in its subtree. Insert walks down like a normal BST and increments size on each node it passes. Duplicates go to the right so they are counted. For `k...