Reported November 2024
AT&Tmath

Counting Triplets Divisible by D

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

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

An array of up to 200000 elements and a triplet count. That's the AT&T OA reported in November 2024, and the input size is the whole story. Brute force over three indices is roughly 10^15 operations at the upper bound, so it dies on the first big test. The way out is remainders modulo d, and d is capped at 2000. If you've got an invite and 48 hours, learn this one shape. StealthCoder is there as a safety net on the live OA if your mind goes blank, but the idea fits on a napkin.

The problem

Given an integer array arr and a positive integer d, count the index triplets (i, j, k) such that 0 <= i < j < k < arr.length and (arr[i] + arr[j] + arr[k]) is divisible by d.
Return the number of valid triplets as a 64-bit integer.

Function
countTriplets(arr: int[], d: int) → long

Examples
Example 1
arr = [3,3,4,7,8]
d = 5
return = 3
The valid one-based index triplets are (1,2,3), (1,3,5), and (2,3,5). Their sums are 10, 15, and 15.
Example 2
arr = [1,2,3,4]
d = 3
return = 2
The triplets using values (1,2,3) and (2,3,4) have sums 6 and 9, both divisible by 3.
Example 3
arr = [-1,0,1,2]
d = 2
return = 2
The sums -1 + 0 + 1 = 0 and -1 + 1 + 2 = 2 are divisible by 2.

Constraints
3 <= arr.length <= 200000.
-10^9 <= arr[i] <= 10^9.
1 <= d <= 2000.
The answer fits in a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Reduce every element to arr[i] mod d, normalized to the range 0 to d-1, because negatives exist here. In most languages -1 % 2 gives -1, so use ((x % d) + d) % d. Now build a count array of size d. Iterate over remainder pairs (a, b), compute c = (-(a+b)) mod d, and enforce a <= b <= c so each multiset is counted once. If all three remainders differ, add cnt[a]*cnt[b]*cnt[c]. If two match, use C(n,2)*other. If all three match, use C(n,3). That's O(d^2), about 4 million steps. The pitfalls are the negative modulo, integer overflow (use 64-bit and compute C(n,3) carefully), and double counting. If you freeze mid-assessment, StealthCoder can hand you the casework while you check it against the examples.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Counting Triplets Divisible by D 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass AT&T's OA.

AT&T reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Counting Triplets Divisible by D FAQ

What's the trick to the AT&T counting triplets problem?+

Group elements by their value mod d, then count combinations of remainders that sum to 0 mod d. You never touch the triplets individually. With d at most 2000, looping over remainder pairs is cheap and the third remainder is determined.

Why does brute force fail here?+

With 200000 elements, three nested loops means on the order of 10^15 checks. Even an O(n^2) approach is 4*10^10 and too slow. The small cap on d is the hint that the solution should depend on d, not n.

How do I handle negative numbers?+

Normalize with ((x % d) + d) % d before counting. Example 3 uses negative values on purpose. If you skip this, remainders like -1 land in the wrong bucket and your counts come out wrong.

Do I need 64-bit integers?+

Yes. The statement says the answer is a signed 64-bit value. Counts multiplied together, like cnt[a]*cnt[b]*cnt[c], overflow 32-bit fast. Use long in Java or C++, and be careful computing n*(n-1)*(n-2)/6 for the all-equal case.

How do I prepare for this in 48 hours?+

Write the remainder-bucket solution once from scratch and test it on the three examples, especially the negative one. Then practice the casework: all distinct, two equal, three equal. That's the part people fumble, so rehearse it until it's automatic.

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

OA at AT&T?
Invisible during screen share
Get it