K Radius Subarray Averages
Smooth out a noisy sensor log. For every position i, take the reading at i plus the k readings on each side of it (2k + 1 readings in all), average them, and round the average down to a whole number. A position whose window would stick out past either end of the log gets -1 instead. Return one answer per reading, in the original order. Limits: 1 to 100,000 readings, each from 0 to 2,000,000,000; k from 0 to 100,000. Think about: how large can the total of one window get? Example (readings 4, 9, 3, 8, 6, 5, 1 with k = 1): index 0 and index 6 each lack a neighbor on one side => -1 index 1 averages 4, 9, 3: total 16, and 16 / 3 rounds down to 5 index 2 averages 9, 3, 8: total 20, and 20 / 3 rounds down to 6 index 5 averages 6, 5, 1: total 12, and 12 / 3 is exactly 4 full answer: [-1, 5, 6, 5, 6, 4, -1]
You're smoothing a noisy sensor log: for each reading, average it with the k readings on either side, rounded down, or answer -1 when one side runs out of neighbors. Interviewers love it because each window overlaps the next one almost entirely. The classic trap is overflow: a few readings near two billion already outgrow a 32-bit integer. You get 1 to 100,000 readings, each from 0 to 2,000,000,000, and k can be anything from 0 to 100,000.
Examples
You write
get_averages(nums: List[int], k: int)
Example 1
- Input
- nums = [4,9,3,8,6,5,1], k = 1
- returns
- [-1,5,6,5,6,4,-1]
Example 2
- Input
- nums = [0,7,2000000000,13,0], k = 0
- returns
- [0,7,2000000000,13,0]
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problemsSliding Window
I reach for it when
- The question asks for the longest, shortest or best stretch of a sequence.
- A rule like "at most k" or "no repeats" decides what counts as valid.
Say this before you type. Say what makes the window valid, and what you do to the left edge when it breaks.
All 9 Sliding Window problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit, and to talk with her for one hour. After that, talking comes with the $79 pack. Anyone can open the editor and run their code.