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
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
binary search tree
order statistics
tree augmentation
rank queries
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 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 Problems
Sample 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...


Submit Your Answer
Markdown supported