Exclusive Time of Functions - Problem

On a single-threaded CPU, we execute a program containing n functions. Each function has a unique ID between 0 and n-1.

Function calls are stored in a call stack: when a function call starts, its ID is pushed onto the stack, and when a function call ends, its ID is popped off the stack. The function whose ID is at the top of the stack is the current function being executed.

Each time a function starts or ends, we write a log with the ID, whether it started or ended, and the timestamp. You are given a list logs, where logs[i] represents the ith log message formatted as a string "{function_id}:{"start" | "end"}:{timestamp}".

For example, "0:start:3" means a function call with function ID 0 started at the beginning of timestamp 3, and "1:end:2" means a function call with function ID 1 ended at the end of timestamp 2.

Note that a function can be called multiple times, possibly recursively.

A function's exclusive time is the sum of execution times for all function calls in the program. For example, if a function is called twice, one call executing for 2 time units and another call executing for 1 time unit, the exclusive time is 2 + 1 = 3.

Return the exclusive time of each function in an array, where the value at the ith index represents the exclusive time for the function with ID i.

Input & Output

Example 1 — Basic Nested Calls
$ Input: n = 2, logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]
Output: [3,4]
💡 Note: Function 0 starts at 0 and ends at 6, total 7 units. Function 1 runs from 2 to 5 (4 units), so function 0's exclusive time is 7-4=3 units.
Example 2 — Sequential Calls
$ Input: n = 1, logs = ["0:start:0","0:start:2","0:end:5","0:end:6"]
Output: [7]
💡 Note: Function 0 has two separate executions: outer call from 0-6 (7 total units) minus inner call from 2-5 (4 units) = 3 units exclusive for outer call, plus 4 units for inner call = 7 total units.
Example 3 — Single Function
$ Input: n = 2, logs = ["0:start:0","0:end:0"]
Output: [1,0]
💡 Note: Function 0 runs from timestamp 0 to 0, which is 1 time unit. Function 1 never executes.

Constraints

  • 1 ≤ n ≤ 100
  • 1 ≤ logs.length ≤ 500
  • 0 ≤ function_id < n
  • 0 ≤ timestamp ≤ 109
  • Two start events will not happen at the same timestamp
  • Two end events will not happen at the same timestamp
  • Each function has an "end" log for each "start" log

Visualization

Tap to expand
Exclusive Time of Functions INPUT n = 2 (functions: 0, 1) logs[]: "0:start:0" "1:start:2" "1:end:5" "0:end:6" Timeline: 0 1 2 3 4 5 6 Func 0 Func 1 ALGORITHM STEPS 1 Parse Logs Extract id, type, time 2 Use Stack Track active functions 3 Process Events Handle start/end 4 Accumulate Time Add exclusive time Stack Trace: t=0: [0] t=2: [0,1] t=5: [0] pop 1 t=6: [] pop 0 f0: +2 f1: +4 f0: +1 done f0: (2-0) + (6-5) = 3 f1: (5-2+1) = 4 FINAL RESULT Output Array: index 0 3 index 1 4 [3, 4] Exclusive Time Breakdown: Func 0: 3 Func 1: 4 t0-1, t6 t2-5 Total: 3 + 4 = 7 units OK - Verified Key Insight: Use a stack to track nested function calls. When a function starts, push it; when it ends, pop it. For each event, calculate the time delta and add it to the currently running function (top of stack). Time Complexity: O(n) where n is number of logs. Space Complexity: O(n) for the stack. TutorialsPoint - Exclusive Time of Functions | Optimal Stack Solution
Asked in
Facebook 45 Amazon 38 Google 32 Microsoft 28
87.2K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen