FastPrepCounting Triplets Divisible by D
Problems/Counting Triplets Divisible by D

Counting Triplets Divisible by D

AT&T logoAT&T● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an AT&T new grad online assessment. The picture in the report shows the divisibility task and an example, but one triplet index it displays does not fit its shown value, and the limits are missing. We added limits. Our version is about 95% the same as the reported question.

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 = 5return = 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 = 3return = 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 = 2return = 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.

More AT&T Online Assessment questions

  • Companies with Average Salary Above 40000OA · Seen Oct 2026
  • Count Proper Prefix MatchesOA · Seen Oct 2026
  • Minimum Binary Swaps to a PalindromeOA · Seen Oct 2026
  • 3SumOA · Seen Oct 2025
  • Break a PalindromeOA · Seen Oct 2025
  • Advertising System Failures ReportOA · Seen Nov 2024
  • Project EstimatesOA · Seen Nov 2024

Similar questions at other companies

  • Get Triplet CountIBM · OAIBM interview questions
  • Count Reverse-Digit PairsVisa · OAVisa interview questions
  • Get Triplet CountPoint72 · OAPoint72 interview questions
  • Count Overlapping Square PairsRamp · OARamp interview questions
  • Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
  • Find Valid SET Card TriplesSnowflake · OA · PHONE SCREENSnowflake interview questions
  • Count Subarrays with a Perfect-Square ProductMicrosoft · OAMicrosoft interview questions
  • Stacked Item and Category DiscountsPalantir · OAPalantir interview questions
  • Two Tribes from Taller-Person CountsIntuit · OAIntuit interview questions
  • Count One-Swap Number PairsCapital One · OACapital One interview questions

Interview reports that mention this question

  • AT&T TDP Software Engineer Questions: 5 Experiences
All AT&T interview questionsSee AT&T hiring insights
Saved
class Solution {
    public long countTriplets(int[] arr, int d) {
      // write your code here
    }
}
Checking account…