KatInterview

Per-Function Self Time from a Call Trace

Stacks & QueuesAuto-graded · 23 test cases
MediumAuto-gradedStacks & Queuesstacksimulationhash-map

Last updated

Sweep a single-threaded call trace with a stack to total each function's self time under half-open intervals, with recursion, strict validation, and name-sorted output.

Per-Function Self Time from a Call Trace

You are given a log of calls made by a single thread. Compute how much time each function spent running itself, meaning time during which it was the innermost active call and not waiting on a function it called.

Input format

trace is a list of events. Each event is a list of exactly three fields:

[name, action, time]

  • name is a nonempty string naming a function.
  • action is exactly "start" or "end".
  • time is a nonnegative integer.

Events appear in nondecreasing order of time.

Rules

  • A "start" event begins a new call, which becomes the innermost active call.
  • An "end" event finishes the innermost active call, and its name must match that call. Recursion is permitted, so the same name may appear several times in the active chain.
  • Time is continuous and every interval is half-open. Between two consecutive events at times a and b, the span [a, b) of length b - a belongs entirely to the innermost active call at that moment. If no call is active, that span belongs to no one.
  • Events with equal timestamps have no time between them and are applied in list order.
  • For example, if A starts at 2, calls B at 5, B ends at 8, and A ends at 10, then B gets 3 and A gets (5 - 2) + (10 - 8) = 5.
  • Add up the self time across all calls of the same name, including recursive calls and calls from different callers.

Output

Return a list of [name, self_time] records, one for each distinct function name that appears in the trace (even if its self time is 0), sorted by name in ascending lexicographic order. An empty trace returns [].

Invalid traces

Raise ValueError if any of the following holds:

  • an event is not a list of exactly three fields;
  • name is not a nonempty string;
  • action is neither "start" nor "end";
  • time is not an integer, is a Boolean, or is negative;
  • a time is smaller than the previous event's time;
  • an "end" arrives when no call is active, or its name differs from the innermost active call;
  • the trace finishes while some call is still active.

Do not modify the input.

Constraints

  • 0 <= len(trace) <= 500_000
  • 0 <= time <= 10**18
  • The trace is expected to start and finish with no active calls.
  • Output is a list of [str, int] pairs.

Examples

Example 1

Input: self_time_per_function([['main', 'start', 0], ['work', 'start', 2], ['work', 'end', 7], ['main', 'end', 10]])

Output: [['main', 5], ['work', 5]]

Example 2

Input: self_time_per_function([['main', 'start', 0], ['child', 'start', 2], ['child', 'end', 8], ['main', 'end', 10]])

Output: [['child', 6], ['main', 4]]

Example 3

Input: self_time_per_function([['f', 'start', 2], ['f', 'start', 4], ['f', 'end', 6], ['f', 'end', 9]])

Output: [['f', 7]]

Hints

Hint 1

Between two consecutive events exactly one call is running: the one on top of the stack, or nobody if the stack is empty.

Hint 2

For each event, first add the elapsed time since the previous event to the function currently on top of the stack, and only then push or pop.

Hint 3

Keep totals in a dictionary keyed by name so recursion and repeated calls merge automatically; register a name on its start so zero-time functions still appear. Check the Boolean-is-an-int trap when validating times.