Kth Smallest Number in Multiplication Table, explained
To find the kth smallest number in an m by n multiplication table, binary search on the value x and count how many table entries are at most x: row i contributes min(x // i, n). The smallest x whose count reaches k is the answer. That is O(m log(mn)) time and O(1) space, with no table built.
By Evin JohnPublished 8 min read
Understanding the task
Picture the times table from school, cut down to m rows and n columns. The cell in row i and column j holds i times j. Many values repeat: 6 shows up as 2 times 3 and as 3 times 2. If you listed all m times n cells in non-decreasing order, what would be at position k, counting from 1? Return that value. m and n can each run into the tens of thousands, so the table can have hundreds of millions of cells.
| Row | Col 1 | Col 2 | Col 3 | Col 4 |
|---|---|---|---|---|
| 1 | 1 | 2 | 3 | 4 |
| 2 | 2 | 4 | 6 | 8 |
| 3 | 3 | 6 | 9 | 12 |
Sorted, those twelve cells read 1, 2, 2, 3, 3, 4, 4, 6, 6, 8, 9, 12. So with m = 3 and n = 4, k = 5 gives 3, k = 8 gives 6, and k = 12 gives 12. With m = 1 and n = 5, the table is just 1 to 5, and k = 4 gives 4. With m = 2 and n = 2, the cells are 1, 2, 2, 4, so k = 3 gives 2.
Approach one: materialize and sort
Generate every product, sort, and index. This is a fine way to check the real solution, and it is the right thing to say first, but at 30,000 by 30,000 it would mean sorting 900 million numbers.
def kth_in_table_slow(m, n, k):
cells = sorted(i * j for i in range(1, m + 1) for j in range(1, n + 1))
return cells[k - 1]A step up uses the fact that each row is sorted. Push the first cell of every row into a min-heap, pop k times, and each time push the next cell from the same row. That is O(k log m) time and O(m) space. Better, but k can be as large as m times n, so the worst case is no better than sorting.
Approach two: count, then binary search the value
Ask a different question: for a value x, how many cells are less than or equal to x? Row i holds i, 2i, 3i and so on, so the cells in row i that are at most x are the multiples of i up to x. There are x // i of them, but never more than n because the row has only n columns. Summing min(x // i, n) over the m rows gives the count in O(m), without touching any cell.
This count rises as x rises. The answer is the smallest x whose count is at least k. Search x between 1 and m times n. If the count at the midpoint reaches k, the answer is the midpoint or smaller; if not, it is larger.
Does the search ever stop on a number that is not in the table, such as 5 or 7 in the 3 by 4 example? No. The count stays flat across gaps and only rises at values that appear. The first x to reach k is always a real cell.
def kth_in_table(m, n, k):
if m > n:
m, n = n, m
small, large = 1, m * n
while small < large:
mid = (small + large) // 2
count = 0
for i in range(1, m + 1):
count += min(mid // i, n)
if count >= k:
large = mid
else:
small = mid + 1
return smallfunction kthInTable(m, n, k) {
if (m > n) [m, n] = [n, m];
let small = 1;
let large = m * n;
while (small < large) {
const mid = Math.floor((small + large) / 2);
let count = 0;
for (let i = 1; i <= m; i++) {
count += Math.min(Math.floor(mid / i), n);
}
if (count >= k) large = mid;
else small = mid + 1;
}
return small;
}The swap at the top is a free speedup. The table is symmetric in m and n, so looping over the smaller dimension gives the same count with fewer iterations. You can also stop the loop early once i exceeds mid, because those rows contribute zero.
Trace with m = 3, n = 4, k = 8
The search range is 1 to 12. The midpoint 6 has a count of min(6, 4) + min(3, 4) + min(2, 4) = 9, which reaches 8, so large becomes 6. The midpoint 3 counts 3 + 1 + 1 = 5, too small, so small becomes 4. The midpoint 5 counts 4 + 2 + 1 = 7, still short, so small becomes 6. The range has collapsed to 6, which matches the sorted list.
How the costs compare
| Method | Time | Space | Practical at 30,000 by 30,000? |
|---|---|---|---|
| Build and sort | O(mn log mn) | O(mn) | No |
| Min-heap over rows | O(k log m) | O(m) | Only for small k |
| Binary search on value | O(m log(mn)) | O(1) | Yes |
Edge cases
- k = 1 always returns 1, and k = m times n returns m times n.
- A single row or a single column: the table is just 1 to n (or 1 to m), so the answer is k.
- Square tables with many duplicates along the diagonal symmetry, such as m = n = 2 with k = 2 or 3, both returning 2.
- Large sizes in languages with 32-bit ints. m times n can reach 900 million, which still fits, but sums in the count can exceed it if you forget the cap at n. Use 64-bit counters to be safe.
Mistakes to avoid
- Forgetting the min with n. Without it, row 1 claims x cells when it only has n, and the count is wrong for every x above n.
- Searching for an exact count of k. Duplicates mean the count can jump from below k to well above it. Look for the first x where the count is at least k.
- Returning `small - 1` or `large + 1` out of habit. With the
small < largeloop andlarge = mid, the answer issmallwhen the loop ends. - Treating it like a sorted matrix search from a corner. The staircase walk from a corner works for counting, but it costs O(m + n) per count, which is fine yet not better, and many candidates get the walk direction wrong under pressure.
- Not checking k against m times n. If k could exceed the table size, ask what to return before you write the search.
Fictional walkthrough
Fictional example
- Candidate
- Building the table is m times n cells, which can be hundreds of millions. Instead I will count, for a value x, how many cells are at most x.
- Candidate
- Row i is the multiples of i, so it contributes x divided by i, capped at n. One pass over the rows gives the count.
- Candidate
- That count only grows with x, so I binary search x from 1 to m times n and return the first value where the count reaches k. It is m times log of mn, and I will loop over the shorter side.
Similar problems on this site
Median of a Row-Wise Sorted Matrix uses the same count-and-search idea but needs a binary search inside each row for the count. Nth Digit shows a monotone count over numbers. In Magnetic Force Between Two Balls, the count is replaced by a greedy placement check. More practice themes are in the algorithm interview questions.
How Natively fits into practice
Practicing from a screenshot? Natively can read the problem in the image and propose an approach, which you can hold up against your own solution once you have one. It also records a written transcript of your mock interview on your computer, so you can review whether you justified the cap at n and the stopping rule. Learn more about an AI interview assistant.
Questions people ask
How do you find the kth smallest number in a multiplication table?
Binary search on the value x from 1 to m times n. Count cells at most x by summing min(x // i, n) over the rows, and return the smallest x whose count is at least k.
What is the time complexity?
O(m log(mn)) time and O(1) space, where m is the smaller side if you swap them. Building and sorting the table instead costs O(mn log mn) time and O(mn) memory.
Why does row i contribute min(x // i, n) cells?
Row i holds the multiples i, 2i, 3i and so on. There are x // i multiples of i up to x, but the row stops after n columns, so the count is capped at n.
Can the binary search return a number that is not in the table?
No. The count only increases at values that appear in the table, so the smallest value whose count reaches k must be a real cell.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
