FastPrepCount Subarrays With Sum Divisible by K
Problems/Count Subarrays With Sum Divisible by K

Count Subarrays With Sum Divisible by K

InMobi logoInMobi● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an InMobi new grad online assessment. Our task and the published limits are about 99% the same as the reported question.

Given an integer array arr and a positive integer k, return the number of non-empty contiguous subarrays whose element sum is divisible by k.

A subarray is a contiguous sequence of elements from arr. A sum s is divisible by k when s % k = 0.

Function

countDivisibleSubarrays(arr: int[], k: int) → long

Examples

Example 1

arr = [2,3,1]k = 2return = 3

The qualifying subarrays are [2], [3,1], and [2,3,1], with sums 2, 4, and 6.

Example 2

arr = [1,2,3,4]k = 1return = 10

Every integer sum is divisible by 1, so all 4 × 5 / 2 = 10 non-empty subarrays qualify.

Example 3

arr = [-1,2,9]k = 2return = 2

The qualifying subarrays are [2], whose sum is 2, and [-1,2,9], whose sum is 10.

Constraints

  • 1 ≤ arr.length ≤ 10^4
  • 1 ≤ k ≤ 10^4
  • -10^9 ≤ arr[i] ≤ 10^9

More InMobi Online Assessment questions

  • Validate a Bracket SequenceOA · Seen Oct 2026
  • Count Substrings Matching with One Adjacent SwapOA · Seen Oct 2026
  • Maximum Subarray with Start and End WindowsOA · Seen Oct 2026
  • Minimum Service Cold StartsOA · Seen Oct 2026
  • Range Maximum Subarray Sum QueriesOA · Seen Oct 2026
  • Connected Components with Divisor ThresholdOA · Seen Aug 2026
  • Maximize Impression ValueOA · Seen Aug 2026
  • Supply Path OptimizationOA · Seen Aug 2026
  • Delivery ServiceOA · Seen Jun 2026
  • Shared InterestOA · Seen Jun 2026

Similar questions at other companies

  • Count Subarrays with a Perfect-Square ProductMicrosoft · OAMicrosoft interview questions
  • Count Zero-Sum FragmentsTesla · OATesla interview questions
  • Count Stable Server SubsegmentsScale AI · OAScale AI interview questions
  • Number of Unique Elements After Modifications 🍁Infosys · OAInfosys interview questions
  • Count Paired Subarrays Reaching a TargetZipRecruiter · OAZipRecruiter interview questions
  • Count Subarrays with Sum KIndeed · OAIndeed interview questions
  • Count Subarrays with Sum Divisible by KVisa · OAVisa interview questions
  • Beautiful SubarraysAtlassian · OAAtlassian interview questions
  • Find Security LevelAmazon · OA
  • Subarray Sum Equals KServiceNow · ONSITE INTERVIEWServiceNow interview questions
All InMobi interview questionsSee InMobi hiring insights
Saved
class Solution {
    public long countDivisibleSubarrays(int[] arr, int k) {
      // write your code here
    }
}
Checking account…