Kth Cheapest Fare in a Distance Tier by Rate Tier Table
Last updated: November 29, 2026
Quick Overview
Fares are tabulated as an m by n table where row i (1-indexed distance tier) and column j (1-indexed rate tier) hold fare i * j. Without building the table, return the k-th smallest value in it.
Uber
November 29, 20260
0
0 solved
Fares are tabulated as an m by n table where row i (1-indexed distance tier) and column j (1-indexed rate tier) hold fare i * j. Without building the table, return the k-th smallest value in it.
An Uber onsite problem that tests binary search over the value domain instead of over indices. It appears when interviewers want to see counting arguments and careful off-by-one handling at scale (m and n up to 30,000).
What the Interviewer Expects
- Rejects materializing m * n values
- Counts how many entries are at most x with sum(min(x // i, n))
- Binary searches for the smallest x whose count is at least k
- Explains why the answer is guaranteed to appear in the table
- States O(m log(mn)) time
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
- Generalize to any matrix whose rows and columns are sorted.
- How would you find the k-th smallest pair distance in an array?
- Can you reduce the counting cost by iterating over the smaller dimension?
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:
m,nup to about ,1 <= k <= m * n. - Output: k-th smallest of all products
i * j. - Building the table would need up to cells, so it is out....
High-Level Design
Define count(x) as the number of table entries at most x. In row i the entries are i, 2i, ..., ni, so min(x // i, n) of them are at most x. Summing over rows gives count(x) in . count is...