I knew a stack was involved pretty quickly but fumbled the part where you have to handle the end timestamp correctly.
Use a stack to track the currently executing function and its start time. When a function starts, push its ID and timestamp; when it ends, pop it, compute its exclusive time by subtracting the start time and any time spent in nested calls, then update the parent's exclusive time accordingly.
Pro tip: Clarify that timestamps are in integer milliseconds and that 'end' timestamp is inclusive, so exclusive time is end - start + 1. Also, mention that the stack approach naturally handles nesting and ensures O(n) time complexity.
Split each log entry into function ID, event type (start/end), and timestamp. Convert timestamp to integer.
Use a stack to store function IDs and their start times, and a dictionary to accumulate exclusive time for each function.
For a 'start' event, push the function ID and timestamp onto the stack. For an 'end' event, pop the top, compute exclusive time as (current timestamp - start timestamp + 1) minus any time already allocated to nested calls, and add to the function's total. Then, if the stack is not empty, subtract this exclusive time from the parent's start time to account for nesting.
When a function ends, ensure that the time spent in its nested calls is excluded from its exclusive time and properly attributed to the nested functions.
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.