Problem
Ask
Submissions

Problem: Exclusive Time of Functions

Medium
30 min
Understand how to compute the exclusive execution time of functions running on a single-threaded CPU by analyzing start and end logs. Explore how to use stacks to track function execution, manage preemption, and accurately calculate time intervals. This lesson helps you master log parsing and stack utilization to solve a crucial coding interview 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.
Problem
Ask
Submissions

Problem: Exclusive Time of Functions

Medium
30 min
Understand how to compute the exclusive execution time of functions running on a single-threaded CPU by analyzing start and end logs. Explore how to use stacks to track function execution, manage preemption, and accurately calculate time intervals. This lesson helps you master log parsing and stack utilization to solve a crucial coding interview 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.