Implement CSAT tracking and call timing
Problem 1: Implement a Rolling CSAT Tracker
Implement a class CSATTracker to manage customer satisfaction (CSAT) scores within a rolling time window.
Each recorded score has:
- A unique
conversation_id(string) - An integer
scorefrom 1 to 5 - An integer
timestamp
The tracker operates with a window_size. A record is considered active at time t if its record_timestamp is greater than or equal to t - window_size.
Your class must implement the following methods:
-
__init__(window_size: int)- Initializes the tracker with the specified window size.
-
record(conversation_id: str, score: int, timestamp: int) -> None- Inserts a new CSAT score.
- After insertion, evicts any records that are no longer active relative to the provided
timestamp.
-
get_average(current_time: int) -> Optional[float]- Before computation, evicts any records older than the active window relative to
current_time. - Returns the average of all currently active scores, rounded to 2 decimal places.
- Returns
Noneif there are no active records.
- Before computation, evicts any records older than the active window relative to
-
update(conversation_id: str, score: int) -> None- Updates the score for an existing active conversation.
- If the conversation has already been evicted from the window, no action should be taken.
- Updating a score must not change its original timestamp or its position in the eviction order.
Assumptions for Problem 1:
conversation_idvalues passed torecordare unique.- Calls to
recordandget_averageoccur 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 executes n functions, identified by integer IDs from 0 to n - 1. You are provided with a list of execution logs, sorted by timestamp. Each log entry is in the format function_id:event_type:timestamp, where event_type is either start or end.
Rules:
- When a function calls another function, the currently running function is paused.
- Functions can be nested to multiple levels.
- An
endevent at timetsignifies that the function finishes at the end of time unitt. This unittcounts towards the function's runtime.
Your task is to return an array answer of size n, where answer[i] represents the exclusive execution time of function i. Exclusive time means time spent executing function i itself, excluding any time spent in functions called by i.
Assumptions for Problem 2:
- The provided logs are valid and represent a properly nested execution trace.
AI interview prep
Core dilemma
The central engineering conflict lies in designing and implementing efficient, stateful systems that accurately process time-series or event-driven data. This requires careful management of dynamic data structures for insertions, evictions, and aggregations (Problem 1), alongside precise handling of time boundaries and nested state transitions (Problem 2), all while ensuring optimal performance and correctness under varying load patterns.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement CSAT tracking and call timing
Problem 1: Implement a Rolling CSAT Tracker
Implement a class CSATTracker to manage customer satisfaction (CSAT) scores within a rolling time window.
Each recorded score has:
- A unique
conversation_id(string) - An integer
scorefrom 1 to 5 - An integer
timestamp
The tracker operates with a window_size. A record is considered active at time t if its record_timestamp is greater than or equal to t - window_size.
Your class must implement the following methods:
-
__init__(window_size: int)- Initializes the tracker with the specified window size.
-
record(conversation_id: str, score: int, timestamp: int) -> None- Inserts a new CSAT score.
- After insertion, evicts any records that are no longer active relative to the provided
timestamp.
-
get_average(current_time: int) -> Optional[float]- Before computation, evicts any records older than the active window relative to
current_time. - Returns the average of all currently active scores, rounded to 2 decimal places.
- Returns
Noneif there are no active records.
- Before computation, evicts any records older than the active window relative to
-
update(conversation_id: str, score: int) -> None- Updates the score for an existing active conversation.
- If the conversation has already been evicted from the window, no action should be taken.
- Updating a score must not change its original timestamp or its position in the eviction order.
Assumptions for Problem 1:
conversation_idvalues passed torecordare unique.- Calls to
recordandget_averageoccur 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 executes n functions, identified by integer IDs from 0 to n - 1. You are provided with a list of execution logs, sorted by timestamp. Each log entry is in the format function_id:event_type:timestamp, where event_type is either start or end.
Rules:
- When a function calls another function, the currently running function is paused.
- Functions can be nested to multiple levels.
- An
endevent at timetsignifies that the function finishes at the end of time unitt. This unittcounts towards the function's runtime.
Your task is to return an array answer of size n, where answer[i] represents the exclusive execution time of function i. Exclusive time means time spent executing function i itself, excluding any time spent in functions called by i.
Assumptions for Problem 2:
- The provided logs are valid and represent a properly nested execution trace.