Compute Exclusive Execution Times
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of nested function execution and precise exclusive-time accounting derived from chronological logs, emphasizing event parsing and maintenance of execution state.
Constraints
- 1 <= n <= 100
- 2 <= len(logs) <= 200000
- 0 <= function_id < n
- 0 <= timestamp <= 1000000000
- logs are in chronological order and form a valid execution trace
- Every start log has a matching end log
Examples
Input: (2, ["0:start:0", "1:start:2", "1:end:5", "0:end:6"])
Expected Output: [3, 4]
Explanation: Function 0 runs from time 0 to 1 for 2 units, pauses while function 1 runs from 2 to 5 for 4 units, then resumes at time 6 for 1 unit. The result is [3, 4].
Input: (1, ["0:start:0", "0:end:0"])
Expected Output: [1]
Explanation: The only function starts and ends at timestamp 0. Since end timestamps are inclusive, it runs for 1 unit.
Input: (2, ["0:start:0", "0:end:1", "1:start:2", "1:end:3"])
Expected Output: [2, 2]
Explanation: There is no nesting. Function 0 runs from 0 to 1 for 2 units, and function 1 runs from 2 to 3 for 2 units.
Input: (2, ["0:start:0", "0:start:2", "0:end:5", "1:start:6", "1:end:6", "0:end:7"])
Expected Output: [7, 1]
Explanation: Function 0 has an outer call and a nested call with the same ID. Its total exclusive time is 2 units before the nested call, 4 units for the nested same-ID call, and 1 unit at timestamp 7. Function 1 runs for 1 unit at timestamp 6.
Input: (3, ["0:start:0", "1:start:2", "1:end:3", "2:start:4", "2:end:7", "0:end:8"])
Expected Output: [3, 2, 4]
Explanation: Function 0 runs at times 0, 1, and 8 for 3 total units. Function 1 runs from 2 to 3 for 2 units. Function 2 runs from 4 to 7 for 4 units.
Hints
- Use a stack to keep track of which function is currently running.
- Track the previous timestamp. On a start log, the current running function has executed until just before the new timestamp. On an end log, remember that the ending timestamp is inclusive.