KatInterview

Reconstruct Call Events from Profiler Snapshots

Stacks & QueuesAuto-graded · 20 test cases
MediumAuto-gradedStacks & Queuesstacksalgorithmsdata-structures

Last updated

Tests stack trace interpretation, event inference from sparse data, and algorithmic reasoning about nesting and correctness.

You are working with output from a sampling profiler that periodically captures the call stack. Each snapshot records a timestamp and the active call stack at that moment, represented as a list where the first element is the outermost (root) function and the last element is the innermost (currently executing) function.

Your task is to reconstruct a sequence of function entry and exit events from these snapshots. Between any two consecutive snapshots, an unknown number of calls and returns may have occurred, but you can infer a plausible sequence by comparing the stacks.

Reconstruction Rules:

  1. For the first snapshot, emit a start event for each function in its stack, from outermost to innermost, all at that snapshot's timestamp.

  2. For each subsequent snapshot, compare its stack with the previous snapshot's stack:

    • Find the longest prefix of positions where both stacks have the same function name (positional matching from index 0).
    • Functions in the previous stack beyond this common prefix have exited: emit end events for them in reverse order (innermost to outermost), all at the current snapshot's timestamp.
    • Functions in the current stack beyond this common prefix have been entered: emit start events for them in forward order (outermost to innermost), all at the current snapshot's timestamp.
  3. After processing all snapshots, leave the final stack's functions open (do not emit end events for them).

Implement a function that takes a list of snapshots and returns a list of events. Each snapshot is a tuple (timestamp, stack) where timestamp is a float and stack is a list of function name strings. Each event is a tuple (timestamp, event_type, function_name) where event_type is 's' for start or 'e' for end.

Important: Function names are compared positionally. If the same function name appears at different depths in the two stacks, they are treated as distinct call frames. For example, recursive calls like ['f', 'f', 'f'] → ['f', 'f'] should correctly end only the deepest 'f'.

Constraints:

  • 0 ≤ len(snapshots) ≤ 10^4
  • Snapshots are ordered by timestamp in non-decreasing order
  • 0 ≤ len(stack) ≤ 100 for each snapshot
  • Function names are non-empty strings
  • All timestamps are non-negative floats

Examples

Example 1

Input: reconstruct_trace([[0, ['a', 'b', 'a', 'c']], [1, ['a', 'a', 'b', 'c']]])

Output: [[0, 's', 'a'], [0, 's', 'b'], [0, 's', 'a'], [0, 's', 'c'], [1, 'e', 'c'], [1, 'e', 'a'], [1, 'e', 'b'], [1, 's', 'a'], [1, 's', 'b'], [1, 's', 'c']]

Example 2

Input: reconstruct_trace([[2.5, ['main', 'work']], [3, ['main', 'work', 'leaf']], [4, ['main']]])

Output: [[2.5, 's', 'main'], [2.5, 's', 'work'], [3, 's', 'leaf'], [4, 'e', 'leaf'], [4, 'e', 'work']]

Hints

Hint 1

Compare stacks position by position from the outermost frame to find where they first differ

Hint 2

Frames that existed in the previous stack but not in the current one (beyond the common prefix) must have exited

Hint 3

The order of events at the same timestamp matters for correct nesting: emit all exits before all entries