← Atlassian Interview Insights
Start by clarifying requirements (e.g., handling initial values, N=0, memory constraints) and then propose a circular buffer (ring buffer) of size N to store the last N values, along with a running sum. After each new value, update the sum by subtracting the oldest value (if buffer is full) and adding the new value, then compute the average as sum / min(count, N). This yields O(1) time per update and O(N) space.
Pro tip: Mention that for ML pipelines, this can be extended to weighted moving averages or exponential moving averages, and that the O(1) update is crucial for real-time feature engineering. Also, note that using a fixed-size array avoids dynamic memory allocation overhead.
Ask about edge cases: what if fewer than N values have arrived? Should the average be over available values or return None? Also confirm N is fixed and positive, and discuss memory constraints.
Propose a circular buffer (array of size N) with a pointer to the oldest element, plus a running sum. Alternatively, a queue (e.g., deque) but circular buffer is more memory efficient.
For each new value: if buffer is full, subtract the oldest value from sum and overwrite it; else increment count. Add new value to sum and store it. Then compute average = sum / min(count, N).
Each update does constant work: one subtraction, one addition, one division, and pointer update. So time complexity is O(1) per update. Space complexity is O(N) for the buffer.
Mention handling of integer overflow (use long or double for sum), thread-safety if needed, and alternatives like maintaining a balanced BST for median but not needed here. Also note that if N is large, O(N) space might be a concern.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Start by clarifying the goal of weighting recent values and the context (e.g., streaming data, concept drift). Then explain EWMA and linear decay with formulas, and compare them to equal-weight rolling average in terms of responsiveness, memory, and bias-variance trade-off. Conclude with a recommendation based on the use case.
Pro tip: Mention that EWMA can be computed incrementally with O(1) memory and is equivalent to a weighted average with exponentially decaying weights, which is often preferred in production for its simplicity and adaptability.
Ask or state the purpose: to emphasize recent data due to non-stationarity or concept drift. Mention the need to balance responsiveness and stability.
Define EWMA formula: S_t = α * x_t + (1-α) * S_{t-1}, where α is smoothing factor. Discuss how α controls the weight decay and effective window size.
Describe a scheme where weights decrease linearly with age, e.g., w_i = (N - i + 1) / sum_{j=1}^N j for a window of size N. Mention that it requires storing the window.
Contrast: equal-weight is simple, unbiased for stationary data, but slow to adapt. EWMA is memory-efficient, highly responsive, but introduces bias towards recent values. Linear decay offers a middle ground but needs a fixed window and more memory.
Suggest EWMA for streaming with limited memory and high adaptability; linear decay for batch with moderate recency emphasis; equal-weight for stationary data where all points are equally informative.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.