Rolling Z-Score of Daily Returns Over a Fixed Window

Last updated: November 29, 2026

Quick Overview

Given a list of daily returns and a window length w, return for each day the z-score of that day's return against the trailing w-day window (including the day), or None until the window is full. Aim for O(n) total time.

Trexquant
Coding & Algorithms
Software Engineer
Trexquant
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given a list of daily returns and a window length w, return for each day the z-score of that day's return against the trailing w-day window (including the day), or None until the window is full. Aim for O(n) total time.

Rolling normalizations like this sit underneath many of the features a systematic fund builds from price data. The interviewer is checking whether you avoid recomputing each window from scratch and whether you think about numerical stability and flat windows.

What the Interviewer Expects
  • Maintains running sum and sum of squares so each step is O(1)
  • Uses the sample standard deviation and states the choice
  • Returns None for incomplete windows and for zero-variance windows
  • Mentions catastrophic cancellation and Welford as a fix
  • Tests on a tiny hand-checked input
Key Topics to Cover
sliding window
running statistics
numerical stability
time series features
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 5000-stock by 3000-day matrix?
  • How do you handle missing days (NaN) inside the window?
  • What changes if you want an exponentially weighted version?
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: returns xs and window w >= 2. Output: list same length; entry i is (xs[i] - mean)/sd over xs[i-w+1..i]. Use sample sd (divide by w-1). Return None while i < w-1 or when sd is effectivel...

High-Level Design

Keep two running accumulators, s = sum(x) and s2 = sum(x^2), over the current window. Each step adds the new value and subtracts the value that just fell out. Mean is s/w and population variance...


Submit Your Answer
Markdown supported

Related Questions