Pair Sum Closest to Zero
Updated
Problem statement
Over all index pairs i != j of nums, let best be the smallest value of |nums[i] + nums[j]|. Return the two values of a pair that reaches best, as [smaller, larger].
Only the indices must differ: a pair with nums[i] == nums[j] is fine.
If more than one pair has the same smallest absolute sum, pick the answer like this:
- Prefer the pair with the smaller sum. For example, a sum of
-3beats a sum of3. - If the sums are also equal, prefer the pair whose smaller value is smaller.
Function
closestPairToZero(nums: int[]) → int[]Examples
Example 1
nums = [15,-6,4,9,-12]return = [-6,4]The pair -6 and 4 has sum -2. Every other pair is farther from zero: for example 15 + (-12) = 3 and 9 + (-12) = -3. So the answer is [-6,4].
Example 2
nums = [5,-9,1,3]return = [-9,5]The sums closest to zero are 5 + (-9) = -4 and 1 + 3 = 4. Both are 4 away from zero, so the pair with the smaller sum wins. That is -4, which gives [-9,5].
Example 3
nums = [-8,3,6,-2,9]return = [-8,9]Two pairs have sum 1: -8 + 9 and 3 + (-2). No pair has sum 0 or -1. The sums are equal, so compare the smaller values: -8 is less than -2. The answer is [-8,9].
Example 4
nums = [-1,6,11,-1]return = [-1,-1]Pairing the two -1 values gives -2. Every pair with 6 or 11 sums to 5 or more (-1 + 6 = 5), so -2 is closest to zero. This pair is valid because its values come from indices 0 and 3. Pairing only different values would give [-1,6] instead, which is wrong here.
Constraints
2 <= nums.length <= 5 * 10^4-10^9 <= nums[i] <= 10^9
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- 4SumAmazon · ONSITE INTERVIEW
- Count Triplets With Sum at Most TargetGoogle · ONSITE INTERVIEWGoogle interview questions
- 3SumOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- 3SumMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Next and Previous Lexicographic PermutationAdobe · ONSITE INTERVIEWAdobe interview questions
- Sort ColorsInfosys · ONSITE INTERVIEWInfosys interview questions
- Four SumPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences