Rolling Window Average over Time Series

PYTHON coding challenge · Difficulty: medium · Topic: Sliding Window · +100 XP

Given a strictly increasing list of timestamps (in seconds) and a corresponding list of numeric values, compute for each timestamp now the average of all values whose timestamp t satisfies now - window <= t <= now. Both ends are inclusive: a value exactly window seconds older than now is still inside the window; one window + 1 seconds older is not. Return a list of these averages, one per input timestamp. This is a common feature engineering step when summarizing event streams in data pipelines.

Example Input

`

timestamps = [1, 2, 3, 10, 11]

values = [10, 20, 30, 40, 50]

window = 3

`

Expected Output

`

[10.0, 15.0, 20.0, 40.0, 45.0]

`

What this PYTHON challenge teaches you

“Rolling Window Average over Time Series” is a medium-level PYTHON challenge focused on Sliding Window. Working through it gives you hands-on practice with deque, prefix sum, time series — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.

Concepts covered

How to approach it

If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:

  1. 1 ≤ len(timestamps) ≤ 100,000
  2. timestamps are sorted in non-decreasing order
  3. timestamps[i] ∈ [0, 10^12]
  4. values[i] ∈ [−10^9, 10^9]
  5. window ∈ [0, 10^12]
  6. Return a list of floats of length len(timestamps)

How to practise it on PySpark.in

Open the challenge, write your Python code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.

Related PYTHON challenges

Helpful resources

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The PYTHON environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.

Is this challenge free?

Yes - the problem, the sample dataset, the hints and unlimited test runs are free.

What level is it?

It is rated medium and covers Sliding Window.

Solve this challenge free on PySpark.in