KatInterview

Self-Time Accounting for Nested Call Traces

Stacks & QueuesAuto-graded · 18 test cases
MediumAuto-gradedStacks & Queuesstackparsingsimulation

Last updated

Use a stack to turn start/end trace records of nested calls on one CPU into each function's self-time, with inclusive end timestamps.

Self-Time Accounting for Nested Call Traces

A single-core processor runs n functions, numbered 0 to n - 1. Only one function executes at any moment. A trace records what happened as a list of strings, each shaped like "id:kind:time":

  • id is the function's number.
  • kind is either start or end.
  • time is an integer timestamp.

A running function may call another function (including itself). The caller is paused while the callee runs and resumes once the callee ends. Calls nest properly, so the most recently started call that has not yet ended is always the one to end next.

Time-unit convention

Time is divided into whole units, where unit t covers the interval from the start of t to the start of t + 1.

  • A start record at time t means the function begins at the beginning of unit t, so it occupies unit t.
  • An end record at time t means the function finishes at the end of unit t, so it also occupies unit t.

A call that starts and ends at the same timestamp t therefore uses exactly one unit.

Task

Return a list of length n where entry i is the self-time of function i: the total number of time units during which function i was the one actually executing. Time spent inside functions it called does not count toward it. If a function is called several times, or calls itself recursively, add up the self-time of all its calls.

Function

Implement self_times(n, records), which takes the number of functions n and the list of trace strings records, and returns the list of self-times indexed by function number.

Constraints

  • 1 <= n <= 100
  • 1 <= len(records) <= 500
  • Timestamps never decrease from one record to the next.
  • The trace is valid: every call ends after any call it made has ended, and every start has a matching end.
  • Every id is in the range 0 to n - 1.

Examples

Example 1

Input: self_times(2, ['0:start:0', '1:start:2', '1:end:5', '0:end:6'])

Output: [3, 4]

Example 2

Input: self_times(1, ['0:start:0', '0:start:1', '0:end:1', '0:end:2'])

Output: [3]

Example 3

Input: self_times(3, ['0:start:0', '1:start:0', '1:end:0', '2:start:1', '2:end:3', '0:end:3'])

Output: [0, 1, 3]

Hints

Hint 1

Because calls nest, the function that is currently executing is always the most recently started call that has not yet ended. Which data structure models that?

Hint 2

Keep a marker for the first time unit that has not yet been credited to anyone. On each record, give the elapsed units since the marker to the function on top of the stack, then update the marker.

Hint 3

Starts and ends differ in how they treat the timestamp. A start at t has not used unit t for the previous function, but an end at t does use unit t for the ending function, so the marker moves to t + 1 after an end.