Compute rolling standard deviation in O(n)
Company: DRW
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Compute rolling standard deviation in O(n) states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- 0 <= len(nums) <= 10^5
- Window size k may be any integer; k <= 0 or k > len(nums) yields an empty result
- Numbers fit in standard floating point; use the sample (k-1) denominator
- Round each window's std to 6 decimal places
Examples
Input: ([1, 2, 3, 4, 5], 2)
Expected Output: [0.707107, 0.707107, 0.707107, 0.707107]
Explanation: Each 2-element window has values differing by 1; sample var = ((-0.5)^2+(0.5)^2)/1 = 0.5, std = sqrt(0.5) ≈ 0.707107.
Input: ([2, 4, 6, 8], 3)
Expected Output: [2.0, 2.0]
Explanation: Window [2,4,6]: mean 4, sample var = (4+0+4)/2 = 4, std = 2.0. Window [4,6,8] is identical by symmetry.
Hints
- Don't recompute the mean and variance from scratch for each window — that is O(n*k). Maintain a running sum S and running sum of squares Q over the current window so each slide is O(1).
- Use the variance identity: sample variance = (Q - S*S/k) / (k - 1), where S is the window sum and Q is the window sum of squares. Take the square root for std.
- Handle the denominator carefully: the interviewer cares that you use k-1 (sample), not k (population). Treat k==1 as std 0, and clamp any tiny negative variance from float error up to 0.