FastPrepPair Sum Closest to Zero
Problems/Pair Sum Closest to Zero

Pair Sum Closest to Zero

Capital One logoCapital One● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Four Capital One interview reports, from late 2021 to late 2022, name the classic closest-to-zero pair-sum task. One of them also asked for a faster version afterwards. The reports give no output format, tie rule or limits. They do not say whether equal values may pair.

Here the two values come from two different positions, and equal values at different positions may pair. They are returned from smaller to larger. On a tie the smaller sum wins, then the smaller first value. The limits are our choices. The editorial's second approach is one faster method.

Our version is about 80% the same as the reported questions.

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:

  1. Prefer the pair with the smaller sum. For example, a sum of -3 beats a sum of 3.
  2. 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
All Capital One interview questionsSee Capital One hiring insights
Saved
class Solution {
    public int[] closestPairToZero(int[] nums) {
        // Write your code here.
    }
}
Checking account…