I knew immediately this was a stack problem but fumbled the timestamp arithmetic for a bit.
Use a stack to track active functions and a variable for the current timestamp. When a function starts, push its ID and start time; when it ends, compute its total elapsed time, subtract the exclusive time of any nested calls, and add the result to the output. This approach handles nesting and ensures exclusive time is correctly calculated.
Pro tip: Clarify the log format and whether timestamps are inclusive or exclusive before coding. Mention that you'll handle edge cases like multiple nested calls and ensure the stack is empty at the end.
Split each log string into function ID, event type (start/end), and timestamp. Convert the timestamp to an integer for calculations.
Create a stack to store active function calls (each entry holds function ID and start time) and a dictionary or list to accumulate exclusive times per function.
For a start event, push the function ID and timestamp onto the stack. For an end event, pop the top entry, compute the total elapsed time, subtract the exclusive time of any nested calls (tracked separately), and add the result to the function's exclusive time.
When a nested function ends, its exclusive time is added to its own record and also subtracted from the parent's total elapsed time. This can be done by maintaining a separate variable for the current function's exclusive time or by adjusting the parent's start time.
After processing all logs, return the exclusive times for each function in the order they first appeared or as specified.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.