Count Viewing Sessions Whose Total Minutes Divide Evenly by K
Last updated: November 29, 2026
Quick Overview
Given a list of per-minute watch increments for a Watch session log (values may be negative after corrections) and an integer k, count the contiguous subarrays whose sum is divisible by k.
Meta
November 29, 20260
0
0 solved
Given a list of per-minute watch increments for a Watch session log (values may be negative after corrections) and an integer k, count the contiguous subarrays whose sum is divisible by k.
A prefix sum plus hash map problem used in Meta coding screens. It extends the familiar count-subarrays-with-sum idea to remainders, and the interviewer watches for correct handling of negative remainders and the initial zero prefix.
What the Interviewer Expects
- Uses prefix sums and the fact that equal remainders mean a divisible difference
- Seeds the remainder map with remainder 0 seen once
- Normalizes negative remainders (or relies on Python's floor modulo and says so)
- Counts pairs via a frequency map in one pass
- Gives O(n) time and O(min(n, k)) space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return whether any subarray of length at least 2 is divisible by k.
- Find the longest such subarray instead of counting them.
- What changes if k can be 0?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: vals, a list of integers, and k > 0. Output: the number of pairs (i, j) with i <= j such that sum(vals[i..j]) % k == 0. Length up to 10^5, so O(n^2) is too slow....
High-Level Design
Let P[t] be the prefix sum of the first t values, with P[0] = 0. A subarray i..j is divisible by k exactly when P[j+1] and P[i] have the same remainder mod k. So scan once, keep a count of e...