mediumStackStack

Exclusive Time of Functions

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

Signals to notice

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

Brute force first

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

The key insight

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

What must stay true

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.

Easy way to go wrong

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

Stack Pattern