CSAT Tracker with Rolling Average, Updates, and Percentiles
Implement record(conversation_id, score, timestamp) to store CSAT scores from 1 to 5.
Implement get_average(current_time) to calculate the average score within a rolling time window.
Remove expired records and return None if the window is empty.
Part 2: Updating Scores
Implement update(conversation_id, score) to modify an existing conversation’s score.
- Only update records still within the active window.
- Ignore expired or nonexistent records.
- Preserve the original timestamp and record order.
Part 3: Percentile Calculation
Implement get_percentile(now, p) to calculate the p-th percentile of active scores.
- Use the nearest-rank method: rank = ceil(p * N / 100).
- Remove expired records before calculating.
- Return
Noneif the window is empty. - Optimize percentile calculation without sorting.
This problem asks you to build a CSAT tracker over a rolling time window. You need to support inserting score records, querying the current average after removing expired entries, updating only active conversations while preserving timestamps and order, and computing the p-th percentile using the nearest-rank method. The key challenge is maintaining windowed statistics efficiently, especially for percentile queries without sorting each time.