Kth Cheapest Fare Stored in a BST of Flight Prices
Last updated: November 29, 2026
Quick Overview
A flight search backend keeps fares in a binary search tree. Return the kth smallest fare (1-indexed), and explain how to support this efficiently when fares are inserted and deleted often.
0
0
0 solved
A flight search backend keeps fares in a binary search tree. Return the kth smallest fare (1-indexed), and explain how to support this efficiently when fares are inserted and deleted often.
A Google onsite question themed on Google Flights price listings. The first part is an in-order walk that stops early; the real test is the follow-up about frequent updates, where the interviewer expects subtree size augmentation.
What the Interviewer Expects
- Use in-order traversal and stop at the kth node
- Avoid building the full sorted list
- Propose storing subtree sizes for O(h) queries
- Explain how sizes are maintained on insert and delete
- Validate k against the tree size
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
- Support insert and delete with O(h) kth queries.
- Return the kth largest instead.
- How would you answer the median in O(h)?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: BST root and 1 <= k <= n. Output: kth smallest value. For the follow-up: frequent inserts and deletes interleaved with kth queries, so a per-query O(n) walk is too slow....
High-Level Design
Part one: iterative in-order traversal yields values in sorted order; decrement k on each visit and return when it hits zero. This costs O(h + k).
Part two: augment each node with size, the node co...