Reported September 2026
Visamath

Sum of Index-Ordered Pair Differences

Reported by candidates from Visa's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Visa OA. Under 2s to a working solution.
Founder's read

The Visa OA reported in September 2026 looks like a pair-counting problem, but it's a one-pass sum in disguise. You see "every pair (i, j)" and n up to 2 * 10^5, and your brain says O(n^2). Don't go there. Each element shows up in a fixed number of pairs, and that count depends only on its index. Once you see that, the code is five lines. If you freeze on the day, StealthCoder is a safety net running invisibly during the live OA, but the trick below is short enough to memorize tonight.

The problem

You are given an integer array arr of length n.
For every pair of indices (i, j) such that 0 <= i < j < n, compute:
arr[j] - arr[i]
Find the sum of these differences over all valid pairs. Return the result modulo 10^9 + 7.
Complete the function sumPairDifferences, which accepts arr and returns the required result. Here, n = arr.length.

Function
sumPairDifferences(arr: int[]) → int

Examples
Example 1
arr = [1, 2, 3]
return = 4
Here, n = 3. All pair differences are:
2 - 1 = 1
3 - 1 = 2
3 - 2 = 1
The sum is 1 + 2 + 1 = 4, and 4 modulo 10^9 + 7 is 4.
Example 2
arr = [4, 1, 3, 2]
return = 1000000003
Here, n = 4. All pair differences are:
1 - 4 = -3
3 - 4 = -1
2 - 4 = -2
3 - 1 = 2
2 - 1 = 1
2 - 3 = -1
The sum is (-3) + (-1) + (-2) + 2 + 1 + (-1) = -4, and -4 modulo 10^9 + 7 is 1000000003.

Constraints
1 <= n <= 2 * 10^5
0 <= arr[i] <= 10^9

Reported by candidates. Source: FastPrep

Pattern and pitfall

Here's what it reduces to. Element arr[k] is the later element j in k pairs (every i before it) and the earlier element i in n-1-k pairs (every j after it). So its net contribution is arr[k] * k - arr[k] * (n-1-k), which simplifies to arr[k] * (2k - n + 1). Sum that over all k and you're done in O(n). The pitfalls are all about modulo. Negative totals must be normalized, so take ((total % M) + M) % M, as Example 2 shows with -4 becoming 1000000003. In languages with fixed-width ints, arr[k] * (2k - n + 1) can hit about 4 * 10^14, which fits in 64 bits but not 32. Reduce as you go. Don't sort the array, because the problem is index-ordered and sorting changes the answer. StealthCoder is your hedge if the formula slips out of your head mid-assessment.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Sum of Index-Ordered Pair Differences 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Visa's OA.

Visa reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Sum of Index-Ordered Pair Differences FAQ

What's the trick for the Visa pair differences problem?+

Count how often each element is added and subtracted. Element at index k is the right side of k pairs and the left side of n-1-k pairs. Its weight is 2k - n + 1. Multiply, sum, and take the modulo at the end. That's O(n) with no nested loop.

How hard is this one really?+

Easy once you see the contribution trick, and medium-feeling if you start with brute force. The math is one line. Most failures come from the negative modulo and overflow, not the algorithm. Test it on both examples before submitting.

Why does Example 2 return 1000000003?+

The true sum is -4. The problem wants a non-negative result modulo 10^9 + 7, so -4 becomes 10^9 + 7 - 4, which is 1000000003. In languages where % can return negatives, add the modulus and mod again.

Can I sort the array to simplify it?+

No. The pairs are defined by index order, i < j, and the difference is arr[j] - arr[i]. Sorting changes which element is on which side and gives a different answer. Only a sorted-input version of the problem allows that shortcut.

How do I prepare for this in 48 hours?+

Practice the contribution-counting idea on a few pair-sum problems, where you ask how many times each element is counted instead of looping over pairs. Then write this solution from scratch twice, handling modulo and 64-bit overflow. That's enough for this pattern.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Visa.

OA at Visa?
Invisible during screen share
Get it