Code RoomTrack pairs by remainder
MediumPrep Room Coding #429

Track pairs by remainder

CodingAlgorithms & data structuresEntry–Mid~16 min

A DJ tool suggests smooth crossfades between tracks whose combined duration lines up with a beat grid. Given a list of track durations in seconds and a grid length k (k >= 1), count the pairs of indices i < j such that durations[i] + durations[j] is divisible by k. For example, with durations [30, 20, 150, 100, 40] and k = 60, the qualifying pairs are (30, 150), (20, 100) and (20, 40), so the answer is 3. Checking every pair is O(n^2) — count by remainder instead.

Implement
divisible_pairs(durations: list[int], k: int) → int
Examples
in[[30,20,150,100,40],60]out3
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 16 min
InputExpectedGot
[[30,20,150,100,40],60]3not run yetsample