AnalystPath

Rolling Z-Score Outlier Detection

PandasHardSenior levelTesla~15 min

Problem

**[Asked at Tesla]**

Use a **5-row rolling window** (min_periods=3) to compute the rolling mean
and standard deviation, then flag rows where the absolute z-score
(`(value - mean) / std`) exceeds **1.5**.

Return columns: `date` (YYYY-MM-DD), `value`, `z_score` (rounded to 2 dp).
Order by date.

Input data

Example rows — the live problem includes the full dataset.

metrics
datevalue
2023-01-01100
2023-01-02102
2023-01-0398
2023-01-04101
2023-01-0599

Expected output

Your answer should return 1 row with the columns date, value, z_score.

Starter code (Pandas (Python))

import pandas as pd

def rolling_outliers(metrics: pd.DataFrame) -> pd.DataFrame:
    # Your code here
    return metrics

Solve this Pandas question free

Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related Pandas questions