Best K-Hour Window by Average Messages Sent
Last updated: November 29, 2026
Quick Overview
Given hourly counts of messages sent on Messenger and a window length k, return the maximum average count over any k consecutive hours.
Meta
November 29, 20260
0
0 solved
Given hourly counts of messages sent on Messenger and a window length k, return the maximum average count over any k consecutive hours.
A fixed-size sliding window warm-up of the kind that appears in Meta's online assessment or as the first part of a screen. The interviewer checks that you update the running sum in O(1) per step instead of re-summing each window.
What the Interviewer Expects
- Computes the first window sum directly
- Slides the window by adding the new hour and subtracting the expired hour
- Tracks the maximum sum and divides once at the end
- Validates that k is between 1 and n
- Gives O(n) time and O(1) 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
- Find the maximum average over windows of length at least k (not exactly k).
- Return the start hour of the best window, breaking ties by earliest.
- Counts arrive as a live stream; report the best window so far after each hour.
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: counts, a list of non-negative integers (one per hour), and k with 1 <= k <= len(counts). Output: the largest mean of any contiguous block of exactly k hours, as a float....
High-Level Design
Sum the first k values. Then for each i from k to n-1, add counts[i] and subtract counts[i-k], which moves the window one hour forward. Keep the best sum. Because every window has the same lengt...