Kth Smallest Number in Multiplication Table

HardBinary Searchbinary search on answer

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

An m x n multiplication table has the value i * j in row i, column j (both **1-indexed**), so it holds m * n numbers, many of them repeated. Given m, n, and k, return the k-th smallest number in the table, counting duplicates (i.e. the value at position k of the sorted list of all m * n entries, 1-indexed). Building and sorting the table is too slow for the largest inputs; aim for about O(m * log(m * n)).

Examples

Input: m = 3, n = 3, k = 5
Output: 3
Sorted entries: 1, 2, 2, 3, 3, 4, 6, 6, 9. The 5th is 3.
Input: m = 2, n = 4, k = 6
Output: 4
Rows are `1 2 3 4` and `2 4 6 8`. Sorted: 1, 2, 2, 3, 4, 4, 6, 8. The 6th is 4.

Constraints

  • `1 <= m, n <= 3 * 10^4`
  • `1 <= k <= m * n`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More binary search on answer problems