Compute moving and weighted averages from a stream
Company: Atlassian
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's understanding of streaming algorithms, sliding-window statistics, and resource-constrained data structure design for computing moving and weighted averages.
Moving Average Over Last X Values
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: (3, [1,10,3,5])
Expected Output: [1.0, 5.5, 4.666667, 6.0]
Explanation: Prompt-style moving average.
Input: (1, [2,4])
Expected Output: [2.0, 4.0]
Explanation: Window one.
Hints
- Use deterministic tie-breaking for prompts with multiple valid outputs.
- For design-style APIs, simulate operations with explicit inputs.
Weighted Moving Average Over Last X Values
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: (3, [1,10,3,5])
Expected Output: [1.0, 7.0, 5.0, 5.166667]
Explanation: Weighted latest values higher.
Input: (2, [4])
Expected Output: [4.0]
Explanation: Single value.
Hints
- Use deterministic tie-breaking for prompts with multiple valid outputs.
- For design-style APIs, simulate operations with explicit inputs.