Kth Smallest Sum from Sorted Matrix Rows
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Amazon OA question, reported in September 2026, is trying to enumerate every row combination. With up to 40 rows and 40 columns, that's astronomically many sums, and it dies on the first big test. The real task is the kth smallest sum when you pick one value per row from sorted rows. It's a heap or BFS-style frontier search, and k is capped at 200 for a reason. If you blank when the timer's running, StealthCoder is the invisible safety net on the live OA. Here's the pattern first.
The problem
Given an integer matrix mat whose rows are sorted in nondecreasing order, choose exactly one value from each row and add the chosen values. Return the kth smallest obtainable sum. Distinct choices occupy distinct ranks even when they produce equal sums. Function kthSmallestRowSum(mat: int[][], k: int) → int Examples Example 1 mat = [[1,3,11],[2,4,6]] k = 5 return = 7 The ordered sums begin 3, 5, 5, 7, 7, so the fifth sum is 7. Example 2 mat = [[1,3,11],[2,4,6]] k = 9 return = 17 There are nine selections, and the largest and ninth ordered sum is 11 + 6 = 17. Example 3 mat = [[1,10,10],[1,4,5],[2,3,6]] k = 7 return = 9 Counting equal sums from different selections separately, the seventh ordered sum is 9. Constraints 1 <= mat.length, mat[i].length <= 40. All rows have the same length and are sorted in nondecreasing order. 1 <= mat[i][j] <= 5000. 1 <= k <= min(200, mat[i].length^mat.length).
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: merge rows one at a time and only ever keep the k smallest sums. Start with the first row as your running list. For each next row, combine the running list with that row, keep the k smallest results, and move on. Because both lists are sorted, you can use a min-heap with index pairs (i, j), pop the smallest, then push (i+1, j) and (i, j+1) with a visited set. Pop k times at most. Complexity is roughly rows * k * log k. The common pitfall is dedupe. Equal sums from different selections count separately, so dedupe on index pairs, never on sum values. Another slip is forgetting to truncate to k after each merge, which blows up the list. If the heap logic slips under pressure, StealthCoder can supply the solution live on the OA without the proctor seeing it.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Kth Smallest Sum from Sorted Matrix Rows cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Kth Smallest Sum from Sorted Matrix Rows FAQ
What's the trick to Kth Smallest Sum from Sorted Matrix Rows?+
Never build all combinations. Merge rows pairwise and keep only the k smallest sums after each merge. Since k is at most 200, every intermediate list stays tiny. A min-heap over index pairs gets you the k smallest pair sums from two sorted lists efficiently.
How hard is this Amazon OA question really?+
Medium-hard. The idea is simple once you see it, but the heap with visited pairs trips people up. If you've done k smallest pairs from two sorted arrays, this is that problem repeated once per row.
Do equal sums count once or multiple times?+
Multiple times. Distinct selections take distinct ranks even when sums match. In example 1 the sums are 3, 5, 5, 7, 7, so the fifth is 7. Track visited by index pair, not by sum value, or you'll skip valid ranks and get the wrong answer.
Can I solve it with binary search instead?+
Yes. Binary search on the sum value and count selections with sum at most mid using DFS that stops once the count passes k. It works, but the early-exit logic is fiddly. The row-by-row heap merge is easier to get right under time pressure.
How do I prepare for this in 48 hours?+
Practice the two-sorted-lists k smallest pairs routine until it's automatic. Then wrap it in a loop over rows, truncating to k each time. Test on the three examples, including the duplicate-sum case. Also check a single-row matrix, where the answer is just the kth element.