MEDIUMasked at 7 companies

Kth Smallest Element in a Sorted Matrix

A medium-tier problem at 64% community acceptance, tagged with Array, Binary Search, Sorting. Reported in interviews at X and 6 others.

Founder's read

You're looking at a matrix where every row and column is sorted. Find the kth smallest element. Sounds straightforward until you realize you can't just flatten and sort, that's O(n²log n) and kills your performance. Meta, Apple, Salesforce, and Oracle all report this one. The trap is thinking you need to explore the whole matrix. The insight is that you don't. If you blank on the binary search angle during your live assessment, StealthCoder runs invisibly and surfaces a working solution in seconds.

Companies asking
7
Difficulty
MEDIUM
Acceptance
64%

Companies that ask "Kth Smallest Element in a Sorted Matrix"

If this hits your live OA

Kth Smallest Element in a Sorted Matrix is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too.

Get StealthCoder
What this means

The obvious move, flatten, sort, return element k, works but wastes time. The real pattern: binary search on the answer space itself. You search for a value between the min (top-left) and max (bottom-right) of the matrix, then count how many elements are smaller than your guess. If the count is less than k, your answer is bigger. If it's greater, your answer is smaller. This cuts you down to O(n log n log(max-min)). Alternatively, a min-heap with smart pointer tracking works too, but binary search is the cleaner path and what most interviewers expect. Common trap: forgetting that row and column sorted doesn't mean the whole matrix is sorted, you can't just walk a diagonal. StealthCoder handles both approaches and picks the right one for your matrix size.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Kth Smallest Element in a Sorted Matrix recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Kth Smallest Element in a Sorted Matrix interview FAQ

Is this really still asked at top companies?+

Yes. Meta, Apple, Salesforce, and Oracle all report it. It appears in most patterns lists for FAANG-style medium difficulty rounds. The acceptance rate sits around 63 percent, so plenty of candidates solve it, but plenty also time out on naive approaches.

What's the trick that separates AC from TLE?+

Binary search on the answer value, not the index. Count elements smaller than your guess using the row-column sorted property, walk from top-right or bottom-left. This is O(n log n log(max-min)). The heap approach is O(k log n) and works fine for small k but scales worse when k is large.

Can I just use a heap and pop k times?+

You can, and it's faster if k is small. Push the top-left cell and neighbors into a min-heap, pop k times, expanding neighbors as you go. It's O(k log n) and avoids binary search complexity. Both approaches are valid. Interviewers like seeing you discuss the trade-off.

How does this relate to Heap and Binary Search?+

The problem tests both patterns. Heap approach: use a priority queue to track the next smallest unvisited cells. Binary Search approach: binary search the range of values and use the sorted structure to count elements. Different interviewers prefer different solutions. Know both to avoid getting locked into one direction.

What if the matrix is huge but k is tiny?+

Heap wins. You only process O(k) cells, not the whole matrix. Push top-left, pop k times, tracking visited cells to avoid duplicates. Binary search is overkill here because you're not gaining from searching a huge space. The heap approach is O(k log k) and gets you out fast.

Want the actual problem statement? View "Kth Smallest Element in a Sorted Matrix" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.