Exclusive Time of Functions

We are executing a program containing 'N' functions on a single-threaded CPU. Each function has a unique 'ID' between 0 and (N-1). Each time a function starts or ends, a log is recorded with the ID, whether it started or ended, and the TIMESTAMP.

Explanation:

You are provided with a 2D array of integers containing information about 'L' logs. Each column in the array represents a log message with the following details:

1st Row - ID of the function.
2nd Row - 1 if the function started, -1 if it ended.
3rd Row - TIMESTAMP of the log.

Your task is to return an array where the value at the ith index represents the exclusive time for the function with ID 'i'.

Input:

The first line of input contains a single integer 'T', representing the number of test cases.
Each test case consists of:
1. A line with two space-separated integers 'N' and 'L'.
2. A line with 'L' space-separated integers for function IDs.
3. A line with 'L' space-separated integers (1 or -1) indicating start or end.
4. A line with 'L' space-separated integers for TIMESTAMPS.

Output:

For each test case, print 'N' space-separated integers representing the exclusive time of each function.

Example:

Input:
0 1 1 1 2 2 1 0
1 1 1 -1 1 -1 -1 -1
0 2 5 7 8 10 11 14
Output:
[5, 7, 3]

Explanation: Process with ID 0 takes 5 units, ID 1 takes 7 units, and ID 2 takes 3 units. The exclusive time is the sum of times for all calls of that function.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 102
  • 2 <= L <= 5 * 102 and L is even.
  • 0 <= TIMESTAMP <= 109

Note:

You do not need to print anything; just implement the function to return the answer.
AnswerBot
1mo

Calculate exclusive time of functions based on logs of function start and end times.

  • Parse the input logs to calculate exclusive time for each function ID.

  • Use a stack to keep track of function start ti...read more

Help your peers!
Add answer anonymously...
Directi Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter