Unique Near-Equal Target-Sum Pairs
Reported by candidates from Fivetran's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Fivetran reported this one in June 2024, and the title sounds scarier than it is. Find unique value pairs that sum to target with absolute difference at most 1. The edge case that breaks a naive solution is the equal-value pair [a, a], which only counts when a shows up twice. If the OA lands in your inbox this week, this is a counting problem wearing a two-sum costume. Most people write a hash-set two-sum and get example 2 or 4 wrong. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the logic here is short enough to hold in your head.
The problem
Given an integer array nums and an integer target, return every unique pair of values whose sum is target and whose absolute difference is at most 1. Each pair must use two distinct array positions. Return each pair as [a, b], where a <= b. Pairs are unique by value, so repeated occurrences do not create duplicate result pairs. An equal-value pair [a, a] is valid only when a occurs at least twice. Sort the returned pairs lexicographically. If no pair qualifies, return an empty array. Function findUniquePairs(nums: int[], target: int) → int[][] Examples Example 1 nums = [1,4,2,3,2] target = 5 return = [[2,3]] Both [1,4] and [2,3] sum to 5, but only [2,3] has an absolute difference at most 1. The repeated 2 does not duplicate the value pair. Example 2 nums = [2,2,1,3] target = 4 return = [[2,2]] The two different positions containing 2 form [2,2]. Although [1,3] also sums to 4, its absolute difference is 2. Example 3 nums = [-3,-1,-2,0] target = -3 return = [[-2,-1]] The pair [-2,-1] sums to -3 and its absolute difference is 1. Example 4 nums = [1,5,9] target = 10 return = [] The values 1 and 9 sum to 10, but their absolute difference is greater than 1. The value 5 occurs only once, so [5,5] cannot be formed. Constraints 0 <= nums.length <= 2 * 10^5 -10^9 <= nums[i] <= 10^9 -2 * 10^9 <= target <= 2 * 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
Here's the trick. Two numbers with difference at most 1 and sum equal to target can only be two shapes. Either a = b, so target must be even and a = target/2, or b = a + 1, so target must be odd and a = (target-1)/2. That means there is at most one candidate pair, ever. Build a frequency map. If target is even, check that count of target/2 is at least 2. If target is odd, check both (target-1)/2 and (target+1)/2 exist. Return [[a,b]] or []. The pitfall is overbuilding with sorting and two pointers, then botching duplicates or the single-occurrence [5,5] case. Watch integer division on negatives, since target can be negative. Use a floor-safe approach, like checking target % 2 carefully in your language. If you freeze on that detail during the live OA, StealthCoder can hand you the clean solution.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Unique Near-Equal Target-Sum Pairs 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Fivetran's OA.
Fivetran reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Unique Near-Equal Target-Sum Pairs FAQ
What's the actual trick in Unique Near-Equal Target-Sum Pairs?+
The difference constraint collapses the search. Sum equals target and difference is 0 or 1, so an even target gives only [t/2, t/2] and an odd target gives only [(t-1)/2, (t+1)/2]. At most one pair can ever exist. Skip the generic two-sum machinery.
How do I handle the equal-value pair case?+
Count occurrences. For an even target, the value target/2 needs a frequency of at least 2, because the two elements must come from distinct positions. One copy of 5 with target 10 returns nothing, as in example 4.
Do negative numbers break anything?+
Yes, division and modulo. In some languages -3 / 2 truncates toward zero and -3 % 2 is -1. Check parity with target % 2 != 0, and compute the lower value as floor division or (target - 1) / 2 only after confirming the target is odd. Example 3 covers this.
What complexity should I aim for?+
O(n) time with a hash map of counts, O(n) space. You could also sort and scan, but there's no need. With nums up to 2 * 10^5, anything quadratic is a fail, so avoid nested loops over pairs.
How do I prepare for this in 48 hours?+
Write the frequency-map solution once from scratch and run all four examples by hand. Then test empty array, a single element, a negative odd target, and a large target near 2 * 10^9 for overflow in your language. That covers nearly every failure case.