KatInterview

Infer Execution Events from Call Stack Snapshots

Stacks & QueuesAuto-graded · 22 test cases
MediumAuto-gradedStacks & Queuesstacks-queuesarraysstrings

Last updated

Reconstruct chronological function START and END lifecycle events by evaluating structural differences across consecutive call stack snapshots.

You are building a execution trace reconstructor for a performance profiler. The profiler periodically captures snapshots of the active call stack at specific timestamps.

Given a list of integer timestamps times and a corresponding list of call stack snapshots stacks, infer the sequence of lifecycle events (START and END).

Each element of stacks represents a snapshot ordered from the root caller (outermost frame, index 0) to the active callee (innermost frame, last index).

To derive events between consecutive snapshots:

  1. Compare the active stack to the preceding stack by finding their longest common prefix. The call stack before the initial snapshot at times[0] is considered empty ([]).
  2. Any frame in the previous stack located beyond the common prefix length has exited. Emit an END event for each such frame, starting from the innermost frame and moving outwards to the common prefix boundary.
  3. Any frame in the current stack located beyond the common prefix length is newly invoked. Emit a START event for each such frame, starting from the prefix boundary and moving innermost (outermost to innermost).
  4. All END events at timestamp $t$ must precede all START events at timestamp $t$.

Each event in the returned list must be formatted as a 3-element list of strings: [timestamp_str, event_type, function_name], where timestamp_str is the integer timestamp converted to a decimal string, event_type is either "START" or "END", and function_name is the string identifier of the function frame.

Active frames present in the final snapshot that do not exit remain open; do not synthesize synthetic END events after the last sample timestamp.

Constraints

  • $0 \le N \le 10^5$, where $N$ is the length of times and stacks.
  • $\text{len(times)} == \text{len(stacks)}$
  • $0 \le \text{times}[i] \le 10^9$, strictly increasing.
  • The total number of frame elements across all snapshots does not exceed $10^6$.
  • Function identifiers are non-empty ASCII strings.
  • Frames with identical names at different stack depths represent separate recursive instances.

Examples

Example 1

Input: infer_stack_events([10, 20, 30], [['main', 'f'], ['main', 'f', 'f'], ['main', 'g']])

Output: [['10', 'START', 'main'], ['10', 'START', 'f'], ['20', 'START', 'f'], ['30', 'END', 'f'], ['30', 'END', 'f'], ['30', 'START', 'g']]

Example 2

Input: infer_stack_events([0], [['main']])

Output: [['0', 'START', 'main']]

Example 3

Input: infer_stack_events([5, 10, 15, 20], [[], ['a', 'b'], ['a'], []])

Output: [['10', 'START', 'a'], ['10', 'START', 'b'], ['15', 'END', 'b'], ['20', 'END', 'a']]

Example 4

Input: infer_stack_events([3, 8], [['a', 'a', 'b'], ['a', 'a', 'b', 'a']])

Output: [['3', 'START', 'a'], ['3', 'START', 'a'], ['3', 'START', 'b'], ['8', 'START', 'a']]

Hints

Hint 1

Frames are identified by their position in the call stack hierarchy (depth + caller chain), not just their function names.

Hint 2

When comparing the current stack to the previous stack, match frames from index 0 upward. The first mismatch marks where frames must be ended and new ones started.

Hint 3

At any given timestamp, end events must process from right-to-left (innermost to outermost) and start events from left-to-right (outermost to innermost) to maintain valid call stack ordering.