KatInterview

Reconstruct Function Entry/Exit Events from Call Stack Snapshots

Stacks & QueuesAuto-graded · 23 test cases
HardAuto-gradedStacks & Queuesstacksimulationstring manipulation

Last updated

Tests ability to compute positional diffs between consecutive call stack snapshots and emit correctly ordered START/END events using a stack-based mental model.

Problem Description

A profiler periodically records the call stack of a running program. Each recording is a list of function names ordered from the outermost caller at index 0 down to the function actively executing at the last index. These recordings are collected at regular intervals and stored in order.

Your job is to reconstruct a minimal sequence of execution events from those recordings. An event is one of:

  • "START <name>" — the function <name> was called
  • "END <name>" — the function <name> returned

The sequence must be consistent with a real execution: functions open and close in a properly nested order, like balanced parentheses.

Producing the Event Sequence

Process the recordings one transition at a time.

Opening transition (before the first recording): Treat the state before any recording as an empty call stack. Emit a START event for each function in the first recording, going from index 0 to the last index.

Between consecutive recordings A and B: Find the length of their longest common prefix — the largest index k such that A[0..k-1] and B[0..k-1] are identical element by element. Then:

  1. Emit END events for every frame in A at index k and beyond, going from the deepest frame back toward index k (leaf to root).
  2. Emit START events for every frame in B at index k and beyond, going from index k toward the deepest frame (root to leaf).

Closing transition (after the last recording): Treat the state after the last recording as returning to an empty call stack. Emit an END event for each function still in the last recording, going from the deepest frame back to index 0.

Important Details

  • Compare frames by position, not by name. If A[i] == B[i] at the same depth they are the same activation; if A[i] != B[i] at depth i, then A[i] and all deeper frames in A have exited, and B[i] and all deeper frames in B are new. This correctly handles recursion (repeated function names at different depths).
  • An empty recording means the program was observed idle at that moment. The entire previous stack closes, and the next non-empty recording opens fresh from the root — even if it happens to contain the same names as before.
  • All END events for the previous recording must appear before any START events for the next recording.

Constraints

  • 0 <= len(samples) <= 10^5
  • 0 <= len(samples[i]) <= 100
  • Each function name is a non-empty string of printable characters with no spaces.
  • Empty recordings may appear anywhere in the list, including at the start or end.

Examples

Example 1

Input: generate_trace([['main', 'f1'], ['main', 'f2']])

Output: ['START main', 'START f1', 'END f1', 'START f2', 'END f2', 'END main']

Example 2

Input: generate_trace([['root', 'worker', 'task'], ['root', 'worker', 'task'], ['root', 'worker']])

Output: ['START root', 'START worker', 'START task', 'END task', 'END worker', 'END root']

Example 3

Input: generate_trace([[], ['main', 'f'], [], ['main']])

Output: ['START main', 'START f', 'END f', 'END main', 'START main', 'END main']

Hints

Hint 1

Think of each transition as a diff between two stacks. The longest common prefix tells you the boundary: everything above it stays untouched, everything below it on the left closes, everything below it on the right opens.

Hint 2

You do not need a separate stack data structure. The previous recording already is your 'active stack'. Track only that reference as you iterate.

Hint 3

Handle the first and last recordings as transitions from/to an empty list. This unifies all three cases — start, middle, end — into the same diff logic and eliminates special-case bugs.