Not sure what to study next?

Get a personalised quant interview roadmap built around your upcoming interviews, strengths and weaknesses.

Takes a few minutes. Start with your target role and firms.

← Programming questions
Medium / PYTHON

Time-Window Moving Average

queues · streaming · time_series 25 minute study target

Not completed

Challenge

Process events [timestamp, value] in the supplied order. After each event at time t, return the arithmetic mean of the events already processed whose timestamps lie in (t-window, t]. The left boundary is excluded. Equal timestamps are allowed; later events at that timestamp are not yet visible. Return the sequence of means.

Input and output contract

0 ≤ n ≤ 1,800; integer timestamps in nondecreasing order; |value| ≤ 10,000; window is a positive integer. Values are equally weighted, not weighted by elapsed time. Empty input returns []. Numeric comparison allows 1e-7 absolute or 1e-9 relative error. w denotes the maximum active event count.

All tests follow this contract. You do not need to validate malformed input. Return the requested result from rolling_moving_average; printed output is not your answer. Standard-library tools are allowed. Implement your solution in Python 3.12.

Target complexity: O(n) time; O(w) auxiliary space plus O(n) output

Return values are checked by type. Use the exact function name. Print output is not your answer. Tests have time and memory limits.

Public examples

Example 1

Arguments

[[[0, 10], [2, 20], [2, 40], [5, 30]], 3]

Expected return

[10.0, 15.0, 23.333333333333332, 30.0]
Example 2

Arguments

[[], 5]

Expected return

[]
Example 3

Arguments

[[[1, 8], [4, 2]], 3]

Expected return

[8.0, 2.0]
Hint

Keep a queue and running sum. Expire events with timestamp ≤ t-window before adding the current event.

The worked solution unlocks after completion, or with Premium access.

rolling_moving_average.pyPython 3.12

Drafts are saved to your account, not to shared browser storage.

Sign in to submit code and save progress.

Results

Your results will appear here.

Hidden tests reveal only their pass/fail status, never their inputs or expected outputs. Runtime is observed wall time—not an inferred complexity class.

Join our Newsletter and Receive the Latest Updates!

Main Links

Trade Courses Assessments Interview Prep Contact Us

Social Media

Facebook Instagram YouTube

Legal

Terms and Conditions Privacy Policy Privacy settings

Talent Acquisition

Looking to hire exceptional quant talent?
See how we can help.

© 2026 EverythingQuant. All Rights Reserved.