Resample Irregular EC2 CPU Readings Into Five Minute Buckets
Last updated: November 29, 2026
Quick Overview
An agent reports EC2 CPU utilization at irregular times (instance_id, ts, cpu_pct). In pandas, convert each instance's readings into a regular five minute series using the bucket mean, forward-fill short gaps up to fifteen minutes, leave longer outages as missing, and flag which values were filled.
Amazon
November 29, 20260
0
0 solved
An agent reports EC2 CPU utilization at irregular times (instance_id, ts, cpu_pct). In pandas, convert each instance's readings into a regular five minute series using the bucket mean, forward-fill short gaps up to fifteen minutes, leave longer outages as missing, and flag which values were filled.
Capacity and autoscaling analyses at AWS need regular time series, but raw agent metrics arrive irregularly and drop out during network blips or instance stops. The interviewer is checking resample semantics, per-group resampling, and the judgment to cap forward-fill so a dead instance does not look healthy.
What the Interviewer Expects
- Resample per instance so buckets never mix instances
- Aggregate within a bucket (mean) before filling
- Forward-fill with a limit derived from the allowed carry time
- Flag imputed values separately from observed ones
- Leave long gaps as NaN and explain why
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- Why might max be a better bucket aggregate than mean for autoscaling decisions?
- How would you extend each series to a common start and end time across instances?
- How would you vectorize this without a Python loop over instances?
- How would you detect instances whose agent stopped reporting entirely?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
readings(instance_id, ts, cpu_pct), irregular timestamps in UTC, several readings can land in one 5 minute bucket.- A bucket's value is the mean of readings inside it. Empty buckets may inherit t...
Approach
For each instance: index by time, sort, resample("5min").mean(). Resampling produces NaN for empty buckets between the first and last reading. Record which are NaN, then ffill(limit=3). The limit ...