Reported September 2026
Visamath

Subarray Sum

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

Visa reported this one in September 2026, and the input size is the whole story. With n up to 2 * 10^5, listing every subarray and summing it is dead on arrival. That's roughly 2 * 10^10 subarrays. The problem is Subarray Sum: add up every element across every contiguous subarray. The hinted pattern is prefix-sum, but the real answer is a counting formula you can write in four lines. If you've got an OA invite, learn the contribution trick and you're done. StealthCoder sits in the background as a safety net if your mind goes blank mid-assessment.

The problem

A subarray is a contiguous segment of an array. Given an array of n integers, determine the sum of all elements across all subarrays of that array.

Function
getSubarraySum(n: int, arr: int[]) → long

Examples
Example 1
n = 3
arr = [4, 5, 6]
return = 50
The subarrays are [4], [5], [6], [4, 5], [5, 6], and [4, 5, 6].
Their total sum is 4 + 5 + 6 + (4 + 5) + (5 + 6) + (4 + 5 + 6) = 50.
Example 2
n = 3
arr = [1, 1, 1]
return = 10
The subarrays are [1], [1], [1], [1, 1], [1, 1], and [1, 1, 1].
Their total sum is 1 + 1 + 1 + (1 + 1) + (1 + 1) + (1 + 1 + 1) = 10.

Constraints
1 <= n <= 2 * 10^5
1 <= arr[i] <= 10^3, where 0 <= i < n

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: stop thinking about subarrays and think about elements. Element at index i (0-based) appears in every subarray that starts at or before i and ends at or after i. That's (i + 1) choices for the start and (n - i) choices for the end. So its contribution is arr[i] * (i + 1) * (n - i). Sum that over all i and you're finished in O(n) time and O(1) space. Check with [4,5,6]: 4*1*3 + 5*2*2 + 6*3*1 = 12 + 20 + 18 = 50. Correct. The common pitfall is overflow. Max total is about 10^3 * (2*10^5)^3 / 6 scale, far past 32-bit, so use a long and cast before multiplying. Another pitfall is writing the O(n^2) prefix-sum loop, which times out. If you freeze on the live OA, StealthCoder can hand you this formula, but you should be able to derive it in a minute.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Subarray Sum 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as sum of all subset xor totals. If you have time before the OA, drill that.

⏵ The honest play

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

Visa 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.

Subarray Sum FAQ

What's the trick for the Visa Subarray Sum problem?+

Count how many subarrays contain each element. Index i (0-based) is in (i + 1) * (n - i) subarrays. Multiply that by arr[i] and add everything up. It's one pass, no nested loops, and it matches both sample outputs of 50 and 10.

Why does brute force fail here?+

With n up to 2 * 10^5, there are about n(n+1)/2 subarrays, which is around 2 * 10^10. Even with prefix sums making each subarray sum O(1), you still iterate all pairs. You need the O(n) contribution formula.

Do I need a long for the answer?+

Yes. The function returns long for a reason. Each term arr[i] * (i + 1) * (n - i) can reach about 10^3 * 10^10, which overflows a 32-bit int. In languages like Java or C++, cast to long before multiplying, not after.

Is prefix sum the right pattern for this?+

It's the hinted pattern, and a prefix-sum double loop gives a correct O(n^2) answer. But it won't pass the constraints. The accepted approach is a per-element counting formula, which is a math and array solution in O(n).

How do I prepare for this in 48 hours?+

Work the formula by hand on [4,5,6] and [1,1,1] until it's automatic. Then code it in your language with long arithmetic and test n = 1 and a max-size array of 1000s. Contribution-counting also shows up in similar subarray and subsequence sum questions.

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