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: [8]
💡 Note: Function 0 has two separate executions: first from 0-1 (2 units) and 6-6 (1 unit), second from 2-5 (4 units). Total: 2+4+1=7 units. Wait, let me recalculate: 0-1 (2 units), 6-6 (1 unit), nested 2-5 (4 units), total 8 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 - Problem OverviewInput Logs0:start:01:start:21:end:5, 0:end:6Stack ProcessingTrack nested callsCalculate durationSubtract nested timeExclusive TimesFunction 0: 3 unitsFunction 1: 4 unitsResult: [3, 4]Timeline visualization: F0 runs 0-1,6-6 (3 units), F1 runs 2-5 (4 units)F0F0F1F1F1F1F0Time: 0 → 1 → 2 → 3 → 4 → 5 → 6Key: Use stack to handle nested calls and calculate exclusive time
Understanding the Visualization
1
Input
Function logs with start/end timestamps
2
Process
Use stack to track nested calls and calculate exclusive time
3
Output
Array of exclusive execution times per function
Key Takeaway
🎯 Key Insight: Use a stack to track function call hierarchy and subtract nested execution time from parent functions to get exclusive time.
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