...

/

Exclusive Execution Time of Functions

Exclusive Execution Time of Functions

Try to solve the Exclusive Time of Functions problem.

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 00 and n1n-1. 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:

  • 11 \leq n 100\leq 100
  • 11 \leq logs.length 500\leq 500
  • 00 \leq function id << n
  • 00 \leq timestamp 103\leq 10^3
  • No two start events and two end events will happen at the same timestamp.
  • Each function has an end log entry for each start log 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

1.

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

A.

[7, 1]

B.

[1, 7]

C.

[7, 0]


1 / 2

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.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

Try it yourself

Implement your solution in main.js in the following coding playground. The supporting code template provided in log.js is meant to assist in developing your solution to the problem.

JavaScript
usercode > main.js
import { Log } from "./log.js";
export function exclusiveTime(n, events) {
// Replace this placeholder return statement with your code
return []
}
Exclusive Time of Functions

Access this course and 1200+ top-rated courses and projects.