AIInterviewTraining logoAIInterview/Training
Coding & DSA / 93

Count the number of contiguous subarrays whose sum is divisible by k.

Testing every subarray costs O(n squared). Prefix sums with a remainder-frequency map solve it in a single O(n) pass: two prefixes sharing a remainder mod k enclose a divisible subarray. The pitfall is negative remainders. Here is the clean answer.

Updated Sep 2026 · Grounded in real GenAI, LLM, and AI/ML engineering interview loops and written to a senior-engineer editorial bar.

Testing every subarray costs O(n squared). Prefix sums with a remainder-frequency map solve it in a single O(n) pass: two prefixes sharing a remainder mod k enclose a divisible subarray. The pitfall is negative remainders. Here is the clean answer.

Unlock the other 847 answers · ₹2,000 / $25Your progress and mastery stay saved · 6 months · one payment · no auto-renew
UP NEXT ON YOUR JOURNEY
DISCUSSION · 0

No comments yet — be the first to share your approach.