Linearly Decayed Moving Average Over a Sparse Price Series
Last updated: November 29, 2026
Quick Overview
Implement a decay operator that, for each day, returns the weighted average of the last d values with weight d for today down to 1 for the oldest, skipping missing values and renormalizing, in O(n) time.
Worldquant
November 29, 20260
0
0 solved
Implement a decay operator that, for each day, returns the weighted average of the last d values with weight d for today down to 1 for the oldest, skipping missing values and renormalizing, in O(n) time.
Linear decay is used on BRAIN-style alphas to smooth signals and cut turnover, and it runs over every instrument and date in a simulation. The interviewer checks whether the candidate can turn the obvious O(n d) loop into a constant time update and keep it correct when data is missing.
What the Interviewer Expects
- Writes the O(n d) brute force first and uses it as a reference
- Derives an O(1) update: every step all weights drop by one, so the weighted sum drops by the plain sum
- Tracks the plain sum, weighted sum, valid count and weight total
- Handles NaN by excluding the value and its weight
- Tests against the brute force on random inputs
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you vectorize this over a matrix of dates by instruments?
- Exponential decay is even cheaper; when would you prefer linear?
- How does floating point drift in running sums show up over 20 years of data, and how would you guard against it?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list x of floats (NaN for missing) and window d. Output at t (for t at least d-1): over valid j in 0..d-1. If no valid values, NaN. Target O(n)....
High-Level Design
Keep S = sum of valid values in the window, W = weighted sum, P = sum of valid weights and k = valid count. Moving one step ages every element: each weight falls by one, so W falls by S and P falls by...