Exclusive Time of Functions - Problem

Imagine you're monitoring a single-threaded CPU that executes a program containing n functions. Each function has a unique ID from 0 to n-1, and function calls follow a stack-based execution model - when a function starts, it's pushed onto the call stack, and when it ends, it's popped off.

You're given execution logs in the format "function_id:start:timestamp" or "function_id:end:timestamp". For example:

  • "0:start:3" means function 0 started at the beginning of timestamp 3
  • "1:end:2" means function 1 ended at the end of timestamp 2

Your task: Calculate the exclusive execution time for each function. This means the total time each function spent actually executing (not waiting for other functions to complete).

Key insight: When a function calls another function, the caller's execution is paused until the callee returns. You need to track which function is actively running at each moment.

Input & Output

example_1.py โ€” Basic Function 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, runs until 2 (2 units), then waits while function 1 runs from 2-5 (4 units), then resumes from 6-6 (1 unit). Total for F0: 2+1=3. Function 1 runs exclusively from 2-5, total: 4 units.
example_2.py โ€” Single Function
$ Input: n = 1, logs = ["0:start:0","0:end:0"]
โ€บ Output: [1]
๐Ÿ’ก Note: Function 0 starts at timestamp 0 and ends at timestamp 0, running for exactly 1 unit of time (from beginning of 0 to end of 0).
example_3.py โ€” Nested Recursive Calls
$ Input: n = 2, logs = ["0:start:0","0:start:2","0:end:5","0:start:6","0:end:6","0:end:7"]
โ€บ Output: [8,0]
๐Ÿ’ก Note: Function 0 has multiple recursive calls. First instance runs 0-1 (2 units), second instance runs 2-5 (4 units), third instance runs 6-6 (1 unit), then first instance resumes 7-7 (1 unit). Total: 2+4+1+1=8. Function 1 never executes.

Constraints

  • 1 โ‰ค n โ‰ค 100
  • 1 โ‰ค logs.length โ‰ค 500
  • 0 โ‰ค function_id < n
  • All function calls are properly nested and matched
  • 0 โ‰ค timestamp โ‰ค 109

Visualization

Tap to expand
Function Execution TimelineF0 StartF1 StartF1 EndF0 EndF0 ActiveF1 ActiveF0Call StackF0F1Stack grows up
Understanding the Visualization
1
Function 0 Starts
Push F0 to stack, start timer at timestamp 0
2
Function 1 Starts
Pause F0 timer (2 units elapsed), push F1 to stack at timestamp 2
3
Function 1 Ends
F1 executed for 4 units (timestamp 2-5), pop F1, resume F0 timer
4
Function 0 Ends
F0 executed for 1 more unit (timestamp 6), total F0: 3 units
Key Takeaway
๐ŸŽฏ Key Insight: Only the function on top of the stack is actively executing - all others are waiting for nested calls to complete!
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
42.0K Views
Medium Frequency
~15 min Avg. Time
1.5K 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