Given periodic snapshots of a call stack from a profiler, reconstruct the full execution trace showing when each function started and stopped.
Each snapshot is a list of function names representing the call stack from bottom (first called) to top (most recent call). Consecutive snapshots are taken at equal time intervals starting at time 0, so their order in the list is their order in time.
By diffing consecutive samples, determine the sequence of events (function entries and exits) that occurred between each pair of snapshots. When moving from one snapshot to the next, find the longest common prefix of the two stacks. Functions above that prefix in the old stack must have exited (top-to-bottom order), and functions above that prefix in the new stack must have entered (bottom-to-top order).
Return a list of events in chronological order, where each event is [function_name, action] with action being either "enter" or "exit". Handle recursive calls correctly by comparing stacks position by position.
Function Signature
def reconstruct_trace(snapshots: list[list[str]]) -> list[list[str]]:
Rules
-
snapshots[i][0]
is the outermost frame and
snapshots[i][-1]
is the most recent call. A snapshot can be empty.
-
The stack before the first snapshot is empty, so every frame in the first snapshot produces an
"enter"
event, bottom to top.
-
The common prefix is found by comparing the two stacks index by index from the bottom, stopping at the first index where the names differ or one stack ends. Every frame above that point in the old stack exits and every frame above it in the new stack enters, even when the same name appears at the same index in both.
-
Within one transition, all
"exit"
events come before all
"enter"
events.
-
Identical consecutive snapshots produce no events.
-
Frames still on the stack after the last snapshot get no
"exit"
event.
-
Events carry no timestamps. Their order in the output is their order in time.
Constraints
-
1 <= len(snapshots) <= 1000
-
0 <= len(snapshots[i]) <= 100
-
Function names consist of lowercase English letters and digits.
-
Each snapshot is a valid call stack, and transitions between consecutive snapshots are consistent.
Examples
Input: snapshots = [["main"], ["main","foo"], ["main","foo","bar"], ["main","foo"], ["main"]]
Output: [["main","enter"],["foo","enter"],["bar","enter"],["bar","exit"],["foo","exit"]]
The stack grows as foo and bar are called, then shrinks as they return. main is still on the stack after the last snapshot, so it has no exit event.
Input: snapshots = [["main","solve"], ["main","solve","solve"], ["main","solve","solve","solve"], ["main","solve","solve"], ["main","solve"], ["main"]]
Output: [["main","enter"],["solve","enter"],["solve","enter"],["solve","enter"],["solve","exit"],["solve","exit"],["solve","exit"]]
The first snapshot already holds two frames, so both enter at the start. solve then calls itself until it is 3 levels deep and unwinds; each recursive frame is tracked by its stack position, so each one gets its own enter and exit.
Input: snapshots = [["main","a","b"], ["main","c","b"], []]
Output: [["main","enter"],["a","enter"],["b","enter"],["b","exit"],["a","exit"],["c","enter"],["b","enter"],["b","exit"],["c","exit"],["main","exit"]]
The second snapshot differs from the first at index 1 (a against c), so the common prefix is only main. Both b and a exit even though b sits at index 2 in both stacks, then c and a new b enter. The empty last snapshot unwinds the remaining three frames from the top.