FastPrepCount Pairs Divisible by K
Problems/Count Pairs Divisible by K

Count Pairs Divisible by K

Apple logoApple● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Apple full-time onsite interview. Counting pairs of indexes, counting repeated values each time, and the sample are about 98% the same as the report. We also chose these: values can be negative, the input can be empty, the count can be large, and the input limits. The report lists the job level as Experienced.

Given an integer array nums and positive integer k, count index pairs (i,j) with i < j for which nums[i] + nums[j] is divisible by k.

Different index pairs count separately, including pairs with equal values. An element cannot pair with itself. Return the count as a signed 64-bit integer.

Function

countDivisiblePairs(nums: int[], k: int) → long

Examples

Example 1

nums = [4,3,5,1,4,5]k = 5return = 3

The two separate occurrences of four each pair with one, and the two fives form one pair.

Example 2

nums = [-1,1,2,-2]k = 3return = 4

The valid sums are zero, negative three, positive three, and zero.

Constraints

  • 0 <= nums.length <= 10^5.
  • -10^9 <= nums[i] <= 10^9.
  • 1 <= k <= 10^9.

More Apple Onsite Interview questions

  • Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • FIFO Mutex Operation TraceONSITE INTERVIEW · Seen Oct 2026
  • Implement Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Parse Nested HTML into a TreeONSITE INTERVIEW · Seen Oct 2026
  • Thread-Safe Concurrent Linked ListONSITE INTERVIEW · Seen Sep 2026
  • Palindrome Match PercentageONSITE INTERVIEW · Seen Sep 2026
  • Binary Tree Nodes at Distance KONSITE INTERVIEW · Seen Aug 2026
  • BST to Sorted Circular Doubly Linked ListONSITE INTERVIEW · Seen Aug 2026

Similar questions at other companies

  • Group-Relative Policy AdvantagesAmazon · ONSITE INTERVIEW
  • Product Total With the Cheapest PromotionInstacart · ONSITE INTERVIEWInstacart interview questions
  • Equation Tuple CombinationZoox · ONSITE INTERVIEW
  • Unique Near-Equal Target-Sum PairsFivetran · ONSITE INTERVIEWFivetran interview questions
  • Minimum Unique Card Suffix LengthBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Digit-Reversed Pairs in an ArrayExpedia · ONSITE INTERVIEWExpedia interview questions
  • Max Points on a LineLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Majority ElementMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Count Index Pairs with Equal Value OffsetGoogle · ONSITE INTERVIEWGoogle interview questions
  • Net Position Calculation from API SnapshotsHeadlands Technologies · ONSITE INTERVIEWHeadlands Technologies interview questions

Interview reports that mention this question

  • Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
  • Apple New Grad SWE Interviews: 37 Experiences (Part 1 of 3)

Apple interview experiences

  • Apple SWE Phone Screen: Subarray Products: 1 Experience
  • Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
  • Apple AI Coding and Related Interviews: 17 Experiences
  • Apple India SWE New Grad Interviews: 5 Experiences
All Apple interview questionsSee Apple hiring insights
Saved
class Solution {
    public long countDivisiblePairs(int[] nums, int k) {
        // Write your solution here.
    }

}
Checking account…