Implement CSAT tracking and call timing
DecagonSoftware Engineer · OnsiteLast updated
This problem tests your ability to manage stateful data during event processing and calculate exclusive execution times from a stream of nested function logs.
Problem Statement
You are building a monitoring service for customer support agents. You are given a sequence of logs from an agent's session. Each log entry is a string in the format "function_id:start:timestamp" or "function_id:end:timestamp".
function_idis an integer identifier.startindicates the function began execution.endindicates the function finished execution.timestampis a non-negative integer representing the exact time the event occurred.
A function is considered to be running from the moment it starts until it ends. If a new function starts while another is running, the parent function is paused, and its exclusive runtime does not include the time spent in the child function.
Implement a function get_exclusive_times(n, logs) where n is the number of functions (IDs 0 to n-1) and logs is a list of strings. Return an array of size n where index i contains the total exclusive time spent in function i.
Constraints
1 <= n <= 1001 <= logs.length <= 500- Logs are strictly ordered by timestamp.
- A function cannot call itself recursively.
Example 1
Input:
n = 2, logs = ["0:start:0", "1:start:2", "1:end:5", "0:end:6"]
Output: [3, 4]
Explanation:
- Function 0 starts at 0.
- Function 1 starts at 2 (Function 0 is paused).
- Function 1 ends at 5 (total time: 5 - 2 + 1 = 4).
- Function 0 resumes at 6 and ends at 6 (total time: (6 - 6 + 1) + 2 = 3).
Example 2
Input:
n = 1, logs = ["0:start:0", "0:end:5"]
Output: [6]
Hints
Hint 1
Use a stack to keep track of the nested function calls, storing the IDs of currently running functions.
Hint 2
When a function starts, calculate the elapsed time since the previous event and attribute it to the function currently at the top of the stack.
Hint 3
When a function ends, add the duration (end_time - start_time + 1) to its total and pop it from the stack, then update the starting point for the next event.
Follow-up questions
- How would your implementation change if logs arrived out of order due to network latency?
- If you needed to store these metrics for millions of agents in real-time, how would you design the data aggregation layer?
- How would you modify the solution to account for functions that are interrupted by system-level asynchronous signals rather than explicit end calls?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.