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
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
binary search on answer
counting
sorted matrix
order statistics
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
  • 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 Problems
Sample Answer
Requirements
  • Input: m, n up to about 3×1043 \times 10^4, 1 <= k <= m * n.
  • Output: k-th smallest of all products i * j.
  • Building the table would need up to 9×1089 \times 10^8 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 O(m)O(m). count is...


Submit Your Answer
Markdown supported