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
Coding & Algorithms
Software Engineer
Worldquant
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
Sliding window
Running sums
Weighted average
Missing data
Time-series operators
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements

Input: list x of floats (NaN for missing) and window d. Output at t (for t at least d-1): ∑j(d−j)xt−j/∑j(d−j)\sum_j (d-j)x_{t-j} / \sum_j (d-j) 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...


Submit Your Answer
Markdown supported

Related Questions