Exclusive Time of Functions
Understand how to determine the exclusive running times of functions in a single-threaded CPU based on start and end logs. Learn to use stack data structures to track function execution slices, manage preemptions, and accurately compute total exclusive times for each function ID.
We'll cover the following...
Statement
We are given an integer number, n, representing the number of functions running in a single-threaded CPU, and an execution log, which is essentially a list of strings. Each string has the format {function id}:{"start" | "end"}:{timestamp}, indicating that the function with function id either started or stopped execution at the time identified by the timestamp value. Each function has a unique ID between and . Compute the exclusive time of the functions in the program.
Note: The exclusive time is the sum of the execution times for all the calls to a specific function.
Constraints:
-
n -
logs.length -
function idn -
timestamp - No two start events and two end events will happen at the same
timestamp. - Each function has an
endlog entry for eachstartlog entry.
Examples
Each function is identified in the logs by a function id. Each log entry is formatted in the following way:
{function id}:{"start" | "end"}:{timestamp}
The above log entries indicate that three functions with IDs 0, 1, and 2 are executed as shown in the following figure. The function with ID 0 started execution at time 0 and ended at time 8. However, since this is a single-threaded CPU, only one function can run at a time. So, when the function with ID 1 starts execution at time 2, the function with ID 0 is preempted. As soon as the function with ID 1 stops execution at time 3, the function with ID 2 starts execution, so the function with ID 0 still remains preempted. Finally, the function with ID 2 ends execution at time 7 and the function with ID 0 resumes and finishes execution at time 8.
Our task is to return the total time for which each function ran. For example, the function with ID 0 ran for a total of 3 time units.
Here are some example inputs and the corresponding expected output:
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Exclusive Execution Time of Functions
Given the following logs and the value of n, what will be the output?
logs [‘0:start:0’, ‘1:start:6’, ‘1:end:6’, ‘0:end:7’]
n 2
[7, 1]
[1, 7]
[7, 0]
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in main.java in the following coding playground. The supporting code template provided in Event.java is meant to assist in developing your solution to the problem.
import java.util.*;public class Main{public static List<Integer> exclusiveTime(int n, List<String> events) {// Replace this placeholder return statement with your codereturn new ArrayList<>();}}