The stack part clicked pretty fast for me but I fumbled the off-by-one on end timestamps for a bit.
Use a stack to track the currently executing function and its start time, and maintain a map from function ID to exclusive time. When an end event occurs, compute the total elapsed time for that function, subtract the time spent in any child functions (tracked via a separate stack or by adjusting the parent's start time), and add the exclusive time to the map.
Pro tip: Clarify whether timestamps are inclusive or exclusive and whether the log is guaranteed to be well-formed; handle edge cases like nested calls and multiple calls to the same function. Mention that you'd use a stack because it naturally models the call stack of a single-threaded CPU.
Identify the structure of each log entry: function ID, event type (start/end), and timestamp. Confirm assumptions about timestamp granularity and log validity.
Use a stack to track active function calls (each entry stores function ID and start time) and a hash map to accumulate exclusive time per function ID.
For a start event, push the function and its start time onto the stack. For an end event, pop the top function, compute its total elapsed time, subtract the time spent in any nested calls (by adjusting the parent's start time), and add the exclusive time to the map.
When a function ends, update the parent function's start time to the current timestamp (or add the child's duration to the parent's start time) so that the parent's exclusive time excludes the child's execution.
After processing all logs, output the map of function IDs to their exclusive execution times. Optionally, sort by function ID if required.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.