Continuous Subarray Sum
Problem
Given an integer array nums of non-negative values and a positive integer k, decide whether there is a contiguous subarray of length at least 2 whose elements sum to a multiple of k (zero counts as a multiple). Return True or False.
- The array is non-empty and has at most 200,000 elements.
- Elements are non-negative and can be as large as a billion.
kis a positive integer and may be much larger than any element or than the total sum.
Examples
nums = [5, 9, 3, 8, 1], k = 7→True.[5, 9]sums to 14.nums = [8, 2, 5], k = 9→False. The candidate sums 10, 7, and 15 are not multiples of 9.nums = [0, 0], k = 4→True.[0, 0]sums to 0, a multiple of any k, and has length 2.
Hints
- A subarray sum is a difference of two prefix sums. When is a difference divisible by k?
- Two numbers have a difference divisible by k exactly when they leave the same remainder modulo k.
- Store the first index at which each remainder of the running prefix sum appears; a repeat at distance at least 2 is the answer.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.