← Meta Interview Insights

Meta·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
May 2026

Summary

Meta ML engineer round, pretty much a single meaty design question about streaming statistics. Felt like a coding question dressed up as a system design one, which threw me a bit.

Questions Asked (1)

Q1

Design a class that tracks a running mean over a stream of numbers with O(1) add and mean operations. Then discuss numerical stability for very long streams and how you'd extend it to a sliding window over the last K elements.

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

I jumped straight to the naive sum-divided-by-count approach, which works fine until they asked about numerical stability for huge streams.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

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).

1. Clarify requirements and constraints

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.

2. Design the basic running mean class

Implement a class with a count and sum, updating both on each add, and returning sum/count for mean. Analyze time and space complexity.

3. Address numerical stability

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.

4. Extend to sliding window of last K elements

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.

5. Discuss trade-offs and optimizations

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.

Key Points to Mention

  • O(1) add and mean using count and sum.
  • Numerical stability: floating-point error accumulation, catastrophic cancellation.
  • Welford's algorithm for stable online mean and variance.
  • Sliding window: deque with running sum, but removal can cause drift.
  • Alternative data structures for sliding window: balanced BST, Fenwick tree, or segment tree for O(log K) operations.
  • Trade-offs between simplicity, time complexity, and numerical stability.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.