MediumParsingPython 3

Stack Trace Reconstructor

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

30m1 sample tests2 hidden tests

Stack Trace Reconstructor

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 do not contain spaces.
  • Use standard-library Python only.

Editor
1
2
Sample Tests
computes exclusive time for nested calls
from solution import exclusive_time

logs = [
    "0 root start",
    "2 parse start",
    "5 parse end",
    "6 fetch start",
    "9 fetch end",
    "10 root end",
]

assert exclusive_time(logs) == {"root": 4, "parse": 3, "fetch": 3}
Results
Run sample tests or submit all tests.