CSAT Tracker
DecagonPhone screenMedium
Customer satisfaction (CSAT) is scored per conversation as an integer from 1 to 5. Build CSATTracker, which keeps scores from a rolling time window and reports their average.
A tracker is created with window_size. At time t, a record is in the window if its timestamp is at least t - window_size; older records are dropped and never come back. The timestamp and current_time values arrive in non-decreasing order, and several records may share a timestamp.
Part 1: Rolling average
Implement CSATTracker(window_size) to create an empty tracker.
record(conversation_id, score, timestamp) drops every stored record older than timestamp - window_size, then stores score for conversation_id at timestamp. It returns nothing.
get_average(current_time) drops every stored record older than current_time - window_size, then returns the average of the remaining scores rounded to 2 decimal places. If no records remain, return None.
- Example 1
- init
[3]- operations
[["record",["conv0",1,1]],["record",["conv1",5,2]],["get_average",[3]],["record",["conv2",4,3]],["get_average",[4]],["record",["conv3",3,4]],["get_average",[4]],["record",["conv4",1,5]],["get_average",[5]]]- Output
[null,null,3,null,3.33,null,3.25,null,3.25]
Why: At time 3, the cutoff is 0, so scores 1 and 5 remain and average 3.0. At time 4, the cutoff is 1; the scores 1, 5, and 4 average 3.33, and adding 3 makes the average 3.25. Recording at time 5 drops the score at timestamp 1 because 1 < 2, leaving 5, 4, 3, and 1.
Constraints
0 <= window_size <= 10^91 <= score <= 50 <= timestamp, current_time <= 10^91 <= len(operations) <= 10^5- Each
conversation_idis recorded at most once. timestampandcurrent_timevalues are non-decreasing across calls.- Several records may share the same
timestamp. get_averagenever returns a value exactly halfway between two 2-decimal values.