My first instinct was to just accumulate raw time per function and I completely forgot to subtract nested call durations.
Use a stack to track active function calls, recording the start time and accumulated child time for each. When an end event occurs, compute the exclusive time as (end_timestamp - start_timestamp + 1) minus the accumulated child time, then add this exclusive time to the function's total and update the parent's accumulated child time. Finally, return the array of exclusive times indexed by function ID.
Pro tip: Clarify the timestamp semantics upfront: whether the end timestamp is inclusive (e.g., start at 0, end at 2 means 3 units) or exclusive. This off-by-one detail is a common pitfall and shows attention to detail.
Confirm timestamp inclusivity, input format, and constraints (e.g., function IDs range, log ordering). Ask about nested calls and whether logs are guaranteed valid.
Use a stack to manage nested calls and an array to accumulate exclusive times. Each stack frame stores function ID, start time, and accumulated child time.
For a start event, push a new frame onto the stack. For an end event, pop the top frame, compute exclusive time, update the function's total, and if the stack is not empty, add the exclusive time to the parent's accumulated child time.
Exclusive time = (end_timestamp - start_timestamp + 1) - accumulated_child_time. Ensure the +1 is applied if timestamps are inclusive.
Return the array of exclusive times. Walk through a simple example to verify correctness, especially for nested calls.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.