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
4mo

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!
Select
Add answer anonymously...

Directi Software Developer interview questions & answers

A Software Developer was asked Q. Stock Buy and Sell Problem Statement Given an array/list 'prices' where the elem...read more
A Software Developer was asked Q. Box Stacking Problem Statement Consider you are provided with 'n' different type...read more
A Software Developer was asked Q. Find Triplets with Given Sum Problem Statement Given an array ARR consisting of ...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Stock Buy and Sell Problem Statement Given an array/list 'prices' where the elem...read more
A Software Developer was asked Q2. Box Stacking Problem Statement Consider you are provided with 'n' different type...read more
A Software Developer was asked Q3. Find Triplets with Given Sum Problem Statement Given an array ARR consisting of ...read more
Directi Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits