FastPrepCount Pairs with Sum K
Problems/Count Pairs with Sum K

Count Pairs with Sum K

Bank of America logoBank of America● EasyNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported by a new grad candidate in a 2022 Bank of America campus drive for a Senior Tech Associate role, as the first of two coding tasks in an online assessment that also included video answers. The candidate had 20 minutes for it. The report shows only one sample input with its answer, not the task text.

Here, the task is read as counting the pairs of positions whose values add up to K. This fits the sample, where one value appears twice and each copy forms its own pair. The sample would also fit counting pairs whose difference is K. The report gives no limits, so we chose up to 10,000 numbers.

Our version is about 70% the same as the reported question.

Given an integer array arr and an integer k, return the number of pairs of positions (i, j) with i < j such that arr[i] + arr[j] = k.

Pairs are counted by position, not by value: if the same value appears at several positions, each position can form its own pair.

Function

countPairs(arr: int[], k: int) → int

Examples

Example 1

arr = [3,4,2,5,4,3]k = 7return = 5

The pairs of positions that add up to 7 are (0, 1), (0, 4), (1, 5) and (4, 5), each a 3 with a 4, and (2, 3), which is 2 + 5. That makes 5 pairs.

Example 2

arr = [2,2,2,2]k = 4return = 6

Any two of the four positions add up to 2 + 2 = 4. There are 4 * 3 / 2 = 6 ways to choose two positions out of four.

Example 3

arr = [-3,9,6,-3,0]k = 6return = 3

Each -3 pairs with the 9: positions (0, 1) and (1, 3). The 6 pairs with the 0: positions (2, 4). No other pair adds up to 6, so the answer is 3.

Constraints

  • 1 <= arr.length <= 10^4
  • -10^6 <= arr[i] <= 10^6
  • -2 * 10^6 <= k <= 2 * 10^6

More Bank of America Online Assessment questions

  • Return Exact Cash Register ChangeOA · Seen Aug 2026
  • Sum Skipping Multiples of 7OA · Seen Nov 2025
  • Wildcard Substring MatchOA · Seen Nov 2025
  • Maximum String BeautyOA · Seen Sep 2024
  • Merge Point of Two Linked ListsOA · Seen Oct 2023
  • Remove Duplicates from a ListOA · Seen Oct 2023
  • Pairs with Target Sum in a Sorted ArrayOA · Seen Oct 2023
  • Count Level-Order Traversals of a TreeOA · Seen Sep 2023
  • Count Set BitsOA · Seen Sep 2023
  • Letter Pattern SumOA · Seen Sep 2022

Similar questions at other companies

  • Count User Mentions in Chat MessagesRamp · OARamp interview questions
  • Count Valid Square PairsAuxoAI · OA
  • Two SumAmazon · OA · PHONE SCREEN
  • Find Words That Can Be Formed by CharactersTabaPay · OA
  • Basic Item DiscountsPalantir · OAPalantir interview questions
  • Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
  • Customer Signer ScreeningState Farm · OA
  • Minimum Magic Stones After MergesElevenLabs · OAElevenLabs interview questions
  • K Most Recent Unique Request IDsMicrosoft · OAMicrosoft interview questions
  • Distinct Products After Removing One ElementHSBC · OAHSBC interview questions

Bank of America interview experiences

  • Bank of America interviews · December 2025
  • Bank of America OA · December 2025
  • Bank of America interviews · September 2026
  • Bank of America OA · September 2026
All Bank of America interview questionsSee Bank of America hiring insights
Saved
class Solution {
    public int countPairs(int[] arr, int k) {
        // write your code here
    }
}
Checking account…