Unique Pairs in a 2D Matrix Summing to Target
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Amazon OA reported in September 2026 looks like a matrix problem, but the grid is a decoy. "Unique Pairs in a 2D Matrix Summing to Target" is just two-sum wearing a costume. Flatten the cells, ignore the geometry, and count pairs that add up to the target. If you're taking this in the next day or two, the hint toward BFS is noise. Nothing here needs traversal. A hash set does the job in one pass. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but this one is easy to hold in your head.
The problem
Given a rectangular integer matrix matrix whose values are globally unique and an integer target, return the number of unordered pairs of values whose sum is target. Each pair must use two different cells. Count a value pair once regardless of order. Function countPairs(matrix: int[][], target: int) → int Examples Example 1 matrix = [[1,5],[7,-1]] target = 6 return = 2 The unordered pairs are (1, 5) and (-1, 7). Example 2 matrix = [[1,2],[3,4]] target = 5 return = 2 The pairs are (1, 4) and (2, 3). Example 3 matrix = [[4]] target = 8 return = 0 The one value cannot be paired with its own cell. Constraints 1 <= matrix.length, matrix[i].length. The matrix is rectangular and contains at most 100000 cells. -10^9 <= matrix[i][j], target <= 10^9. All matrix values are distinct.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Here's the reduction. Values are globally distinct, so every value appears in exactly one cell. That means you never worry about duplicates or about reusing a cell, except one case: a value pairing with itself when target equals twice the value. Since values are unique, that self-pair is impossible with only one copy, so the set check handles it by checking the complement before inserting the current value. Loop over every cell, compute target minus value, and if it's in the set, increment the count. Then add the value to the set. Time is O(n) over at most 100000 cells, space is O(n). The common pitfall is writing nested loops over rows and columns, which is O(n^2) and times out. The other trap is counting each pair twice. Checking before inserting avoids that. If you freeze on the OA, StealthCoder can hand you this approach live, but the pattern is short enough to memorize.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Unique Pairs in a 2D Matrix Summing to Target 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.
Unique Pairs in a 2D Matrix Summing to Target FAQ
What's the trick in the Amazon unique pairs matrix problem?+
Ignore the 2D shape. Treat the matrix as a flat list of distinct numbers and solve two-sum with a hash set. For each value, check whether target minus value was already seen, count it, then insert the value. One pass, no grid traversal.
Do I need BFS or DFS for this one?+
No. The matrix layout is irrelevant because pairs can come from any two different cells, not adjacent ones. BFS would only add complexity. A nested loop over rows and columns feeding a hash set is all you need.
How do I avoid counting a pair twice?+
Check the complement against the set before you insert the current value. Each unordered pair gets counted only when the second of its two values is processed. Since all values are distinct, there's no duplicate-value edge case to worry about.
What complexity will pass with 100000 cells?+
O(n) time and O(n) space, where n is the number of cells. A brute-force check of all pairs is O(n^2), roughly 10^10 operations at the limit, so it won't pass. Use the hash set approach.
How do I prepare for this in 48 hours?+
Write two-sum with a hash set from memory a few times, then practice flattening a 2D array in your language of choice. Test on the examples, including the single-cell case returning 0. Watch for large values near 10^9 and negatives.