The stack simulation part clicked pretty fast for me.
Use a stack to track the currently executing function and maintain a running total of exclusive time. When a function starts, push it onto the stack; when it ends, pop it and add the elapsed time to its exclusive time, then subtract that elapsed time from the parent function's exclusive time if the stack is not empty.
Pro tip: Clarify the timestamp semantics upfront: whether the end timestamp is inclusive or exclusive, and whether the start and end timestamps are in the same unit. This avoids off-by-one errors and shows attention to detail.
Ask whether timestamps are integers, whether the end timestamp is inclusive, and if there are any edge cases like nested calls or multiple top-level functions.
Use a stack to maintain the call hierarchy. Each stack frame stores the function ID and the start time (or last resume time) of that function.
For a start event, push the function and its start time onto the stack. For an end event, pop the function, compute its exclusive time as (end_time - start_time + 1) if inclusive, add to its total, and update the parent's start time to end_time + 1.
When a function ends, if the stack is not empty, the parent's exclusive time should exclude the child's execution time. This is naturally handled by updating the parent's start time to the child's end time + 1.
Collect exclusive times for each function ID, ensuring the output is in the order of function IDs (e.g., 0 to n-1).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.