KatInterview

Reconstruct Function Lifetimes from Periodic Stack Snapshots

Stacks & QueuesAuto-graded · 17 test cases
HardAuto-gradedStacks & Queuesstacksimulationtwo-pointers

Last updated

Tests ability to diff consecutive call-stack snapshots using longest-common-prefix analysis and stack unwinding to produce ordered START/END lifecycle events.

Problem Description

A runtime tracer periodically records the active call stack of a running program. You receive these recordings as a sequence of (timestamp, stack) pairs, where each stack is a list of function names ordered from the outermost caller at index 0 to the deepest active frame at the last index. You also receive a shutdown_time marking when the program terminates.

Your task is to turn these periodic snapshots into a complete, ordered sequence of function lifecycle events — one "START" event when a function frame is entered and one "END" event when it returns.

Reconstruction Rules

  1. Before the first snapshot, treat the active stack as empty.
  2. At each snapshot's timestamp, compare the stack recorded there against the stack from the previous snapshot (the previous stack). Find the longest common prefix (LCP): the deepest index k such that every position from 0 through k-1 holds the same function name in both stacks. The comparison is strictly positional — two frames match only when they share both the same name and the same depth index.
  3. Every frame in the previous stack at depth k or deeper has exited. Emit "END" events for those frames in inside-out order (deepest frame first, working toward depth k), all at the current timestamp.
  4. Every frame in the current stack at depth k or deeper is a fresh entry. Emit "START" events for those frames in outside-in order (from depth k toward the deepest new frame), all at the current timestamp.
  5. When shutdown_time is reached, close every frame still on the stack by emitting "END" events in inside-out order.

Each event is a tuple with four fields: (timestamp, event_type, function_name, depth), where depth is the 0-based index of that frame in the stack.

At any single timestamp, all "END" events come before all "START" events.

Constraints

  • 0 <= len(samples) <= 10^4
  • Timestamps in samples are strictly increasing integers.
  • shutdown_time > samples[-1][0] when samples is non-empty.
  • Each stack is a list of zero or more non-empty alphanumeric strings.
  • The same function name may appear at multiple depths in the same stack (recursive calls).
  • Stack depth does not exceed 300.

Examples

Example 1

Input: reconstruct_call_events([[10, ['main', 'foo']], [20, ['main', 'bar']]], 30)

Output: [[10, 'START', 'main', 0], [10, 'START', 'foo', 1], [20, 'END', 'foo', 1], [20, 'START', 'bar', 1], [30, 'END', 'bar', 1], [30, 'END', 'main', 0]]

Example 2

Input: reconstruct_call_events([[100, ['eval', 'eval', 'eval']], [150, ['eval']]], 200)

Output: [[100, 'START', 'eval', 0], [100, 'START', 'eval', 1], [100, 'START', 'eval', 2], [150, 'END', 'eval', 2], [150, 'END', 'eval', 1], [200, 'END', 'eval', 0]]

Hints

Hint 1

Start by asking: between two consecutive snapshots, which frames are shared and which have changed? The longest common prefix tells you exactly where the two stacks diverge — everything at or above that boundary is affected, everything below it is not.

Hint 2

When frames need to close (END events), think about which frame must exit first. A child frame always exits before its caller, so work from the deepest frame back toward the divergence point. When new frames open (START events), callers must start before their callees, so go from the divergence point down to the deepest new frame.

Hint 3

Treat shutdown_time as a final snapshot with an empty stack. That single observation handles all still-open frames at program exit without any special-case code. Also consider what happens with recursive calls — why does positional comparison handle them correctly where a name-only comparison would not?