I went straight to a circular buffer with a running sum and that part landed fine.
Start by clarifying the problem and constraints, then propose a circular buffer (ring buffer) to store the last k values, maintaining a running sum for O(1) updates. Discuss precision (e.g., using double or Kahan summation) and overflow (e.g., using long for sum), and outline a testing strategy covering edge cases like k=1, large values, and window not yet full.
Pro tip: Mention that in ML pipelines, rolling averages are often used for smoothing metrics; emphasizing numerical stability and thread-safety shows production awareness. Also, proactively discuss how you'd handle k=0 or negative input, as interviewers at Meta value defensive coding.
Ask about input types (int, float), expected precision, and whether k can be zero or negative. Confirm that next(val) is called sequentially and that we need O(1) time per call.
Propose a circular buffer of size k to store the last k values, along with a running sum. Explain that this gives O(1) time per next() and O(k) space.
Discuss using double for the average and long for the sum to avoid overflow. Mention Kahan summation or compensated summation if high precision is needed.
Explain behavior when fewer than k values have been seen (e.g., average of all seen so far). Handle k=0 or negative by throwing an exception or returning 0.
List test cases: k=1, k>1, window not full, window full, large values, negative values, zero values, and precision checks. Suggest unit tests and property-based testing.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.