My first instinct was right, use a stack to track which function is currently running.
Use a stack to track the currently executing function and its start 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 update the parent's start time to account for the child's execution. This ensures each function's exclusive time excludes time spent in nested calls.
Pro tip: Clarify that timestamps are inclusive and that the CPU is single-threaded, so no concurrency issues arise. Also, mention that you'll handle nested calls correctly by adjusting the parent's start time after a child completes.
Convert each log entry into a tuple (id, type, timestamp) and initialize a stack to track active functions and a dictionary to accumulate exclusive times.
Iterate through the logs: if it's a start, push the function id and timestamp onto the stack; if it's an end, pop the top, compute the elapsed time, add to the function's exclusive time, and update the new top's start time to the current timestamp + 1.
When a function ends, ensure that the parent function's start time is adjusted to exclude the child's execution time, so the parent's exclusive time is accurate.
After processing all logs, return the dictionary mapping function ids to their exclusive times, ensuring all functions are included even if they have zero exclusive time.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.