Knew I needed a stack pretty quickly but fumbled the off-by-one stuff with the timestamps.
Use a stack to track the currently executing function and maintain a running total of exclusive time. When a start event occurs, add the elapsed time since the last event to the top function's exclusive time, then push the new function. When an end event occurs, add the elapsed time to the top function's exclusive time, pop it, and update the last timestamp.
Pro tip: Clarify whether timestamps are inclusive or exclusive and how nested calls are logged; this affects the off-by-one handling. Also, mention that the stack approach naturally handles arbitrary nesting depth.
Ensure logs are sorted by timestamp. If not, sort them first. Parse each log into (function_id, event_type, timestamp).
Use a stack to keep track of active functions and a dictionary to accumulate exclusive time per function. Initialize last_timestamp to the first event's timestamp.
Iterate through logs. For a start event, add (timestamp - last_timestamp) to the exclusive time of the function on top of the stack (if any), then push the new function. For an end event, add (timestamp - last_timestamp + 1) to the top function's exclusive time, pop it, and update last_timestamp.
After each event, set last_timestamp to the current timestamp. For end events, ensure the time slice includes the current timestamp (hence +1 if timestamps are inclusive).
After processing all logs, return the dictionary mapping function IDs to their exclusive execution times.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.