Use a stack to track the call hierarchy, recording each function's start time and accumulating child time. When a function ends, compute its exclusive time as (end - start + 1) minus the total time spent in its children, then add that exclusive time to the function's total and propagate the child time to the parent.
Pro tip: Clarify the timestamp semantics upfront: timestamps are inclusive and represent discrete time units, so duration is end - start + 1. Also, explicitly handle nested calls and ensure the stack correctly manages parent-child relationships to avoid off-by-one errors.
Parse each log entry into function ID, event type (start/end), and timestamp. Clarify that timestamps are inclusive and represent discrete time units.
Use a stack to maintain the current call stack, and an array to store exclusive times for each function. Optionally, maintain a separate stack for accumulated child time per function.
For a 'start' event, push the function ID and its start timestamp onto the stack. For an 'end' event, pop the function, compute its exclusive time by subtracting child time from the total duration, update its exclusive time, and propagate the child time to the parent if any.
When a function ends, calculate total duration as (end_timestamp - start_timestamp + 1). Subtract the sum of exclusive times of all direct children (tracked separately) to get the function's exclusive time.
After processing all logs, return the array of exclusive times for each function in order of function ID.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.