The stack approach clicked for me pretty quickly but I fumbled the timestamp math at first.
Use a stack to track the currently executing function and its start time. When a new function starts, add the elapsed time since the previous start to the previous function's exclusive time, then push the new function. When a function ends, add the elapsed time to its exclusive time and pop the stack, updating the start time for the next function on the stack.
Pro tip: Clarify whether timestamps are inclusive or exclusive, and handle the end timestamp carefully by adding 1 if the end is inclusive. Also, consider edge cases like nested calls and ensure the stack is empty at the end.
Split each log entry into function ID, event type (start/end), and timestamp. Convert the timestamp to an integer.
Create an array of size n to store exclusive times, initialized to 0. Use a stack to keep track of function IDs and their start times.
For a start event: if the stack is not empty, add the time elapsed since the top function's start to its exclusive time. Then push the new function ID and its start time. For an end event: pop the function, add the elapsed time (including the end timestamp) to its exclusive time, and if the stack is not empty, update the start time of the new top function to the current timestamp + 1.
After processing all logs, return the array of exclusive times.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.