You were asked to solve two coding problems.
Problem 1: Implement a rolling CSAT tracker
Implement a class CSATTracker for customer-satisfaction scores.
Each recorded score:
-
belongs to a unique
conversation_id
-
is an integer from 1 to 5
-
has an integer timestamp
The tracker uses a rolling window of size window_size. A record is active at time t if record_timestamp >= t - window_size.
Implement:
-
__init__(window_size: int)
-
record(conversation_id: str, score: int, timestamp: int) -> None
-
Insert the score.
-
After insertion, evict any records that are no longer in the active window relative to
timestamp
.
-
get_average(current_time: int) -> Optional[float]
-
Before computing the result, evict any records older than the active window relative to
current_time
.
-
Return the average active score rounded to 2 decimal places.
-
Return
None
if there are no active records.
-
update(conversation_id: str, score: int) -> None
-
Update the score for an existing active conversation.
-
If that conversation has already been evicted, do nothing.
-
Updating a score must not change its timestamp or its position in eviction order.
Assumptions:
-
conversation_id
values passed to
record
are unique.
-
Calls to
record
and
get_average
occur in non-decreasing timestamp order.
-
The goal is to support these operations efficiently without scanning all stored records on every call.
Problem 2: Compute exclusive runtime from nested call logs
A single-threaded CPU runs n functions identified by IDs 0 to n - 1. You are given a list of execution logs sorted by timestamp. Each log has the form function_id:start:timestamp or function_id:end:timestamp.
Rules:
-
A function may call another function, which pauses the currently running one.
-
Functions may be nested multiple levels deep.
-
An
end
event at time
t
means the function finishes at the end of time unit
t
, so that unit counts toward the function's runtime.
Return an array answer where answer[i] is the exclusive execution time of function i, excluding time spent in any child calls.
You may assume the logs are valid and represent a properly nested execution trace.