Detect Anomalies Using Sliding‑Window Z‑Score
PYTHON coding challenge · Difficulty: medium · Topic: Sliding Window · +100 XP
A pipeline monitors a daily metric and flags days that look unusual compared with the days just before them.
For every day i with i >= window, take the previous window values data[i-window:i] (day i itself is NOT included), compute their mean and their population standard deviation (divide by window), and compute the z-score z = (data[i] - mean) / std. Day i is an anomaly when abs(z) > threshold (strictly greater).
If the previous window has a standard deviation of 0 (all values equal), day i is an anomaly exactly when its value differs from that window's mean. Return the 0-based indices of all anomalies in increasing order (an empty list if there are none).
Example Input
`
data = [10, 12, 11, 13, 12, 30, 12, 11, 13, 12, 2, 12]
window = 4
threshold = 2.0
`
Expected Output
`
[5, 10]
`
Day 5 (30) is far above its previous four days [12, 11, 13, 12] and day 10 (2) is far below [12, 11, 13, 12].
What this PYTHON challenge teaches you
“Detect Anomalies Using Sliding‑Window Z‑Score” is a medium-level PYTHON challenge focused on Sliding Window. Working through it gives you hands-on practice with sliding window, z-score, statistics, anomaly detection — 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
- sliding window
- z-score
- statistics
- anomaly detection
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:
- Start at i = window: earlier days do not have a full previous window.
- The window for day i is data[i - window:i] -- it does not include day i itself.
- Population standard deviation divides by window (statistics.pstdev), not window - 1.
- Guard against std == 0: then any value different from the mean is an anomaly.
- The comparison is strict: a z-score exactly equal to the threshold is not an anomaly.
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
- Rolling Window Average over Time Series
- Word Frequency Counter
- Validate Unique User IDs
- Extract Error Codes from Log Strings
- Sum of Values by Key
- Detect Anomalies in Transaction Batches
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.