MediumParsingPython 3

Stack Trace Reconstructor

Reconstruct exclusive function runtimes from nested start/end profiler events.

30m3 sample tests5 hidden tests

Implement exclusive_time(logs) for a profiler event stream. Each log line has the form:

text
1<timestamp> <function_name> <start|end>

Timestamps are integer monotonic ticks. end timestamps are exclusive: if the active function starts at 2 and ends at 5, it ran for 3 ticks.

Requirements

  • Return a dictionary mapping function name to exclusive runtime.
  • Support nested function calls with a stack.
  • Support repeated calls to the same function.
  • Preserve deterministic behavior for valid, sorted input.
  • Raise ValueError for mismatched end events.

Example

python
1logs = [ 2 "0 root start", 3 "2 parse start", 4 "5 parse end", 5 "6 fetch start", 6 "9 fetch end", 7 "10 root end", 8] 9 10assert exclusive_time(logs) == {"root": 4, "parse": 3, "fetch": 3}

Constraints

  • Assume input is already sorted by timestamp.
  • Function names don't contain spaces.
  • Use standard-library Python only.

Editor