mediumStackStack

Exclusive Time of Functions

mediumTime: O(n)Space: O(n)

Recognize the pattern

track execution time of nested function callsexclusive time per functionstack for call hierarchy

Brute force idea

Not applicable — the stack approach IS the natural solution for nested calls.

Better approach

Stack of function IDs. On start: push function, record timestamp. On end: pop, compute duration, subtract from parent's time. O(n).

Key invariant

The stack represents the active call chain. When a function ends, its duration is added to its total. The parent function's exclusive time excludes child durations.

Watch out for

Not subtracting child time from parent — exclusive time means time spent directly in the function, not in its children.

Stack Pattern