I jumped straight to the naive sum-divided-by-count approach, which works fine until they asked about numerical stability for huge streams.
Start by designing a class that maintains a count and a running sum, updating both on each add and computing the mean as sum/count in O(1). Then discuss numerical stability issues with large sums and propose a stable alternative like Welford's algorithm. Finally, extend to a sliding window using a deque and a running sum, or a more stable approach like a balanced binary search tree or a Fenwick tree for O(log K) operations.
Pro tip: Mention that while the naive sum approach is O(1), it can suffer from catastrophic cancellation and loss of precision; Welford's method is more stable but still O(1) per update. For sliding windows, highlight the trade-off between simplicity (deque with sum) and stability (using a data structure that supports removal without accumulating error).
Confirm that add and mean must be O(1), and discuss the expected stream length and precision requirements. Ask if the stream is potentially infinite or very large.
Implement a class with a count and sum, updating both on each add, and returning sum/count for mean. Analyze time and space complexity.
Explain how floating-point errors accumulate with large sums, and introduce Welford's online algorithm for a more stable mean and variance. Discuss the trade-offs.
Propose using a deque to maintain the window and a running sum, but note that subtraction can cause drift. Alternatively, suggest a balanced BST or Fenwick tree for O(log K) updates and queries with better stability.
Compare the O(1) naive approach with O(log K) stable approaches, and mention potential optimizations like periodic recomputation or using a circular buffer with a sum.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.