KatInterview

Stack Trace Event Stream from Periodic Samples

Stacks & QueuesModel answer
MediumModel answerStacks & Queuesstacksarrayssimulation

Last updated

Convert a sequence of call stack snapshots into function entry and exit events with configurable debouncing to filter transient frames.

You're building a profiler that periodically captures snapshots of an application's call stack. Your task is to reconstruct the timeline of function entries and exits from these snapshots.

You receive a sequence of samples in chronological order. Each sample is a pair (timestamp, stack) where the stack is an ordered list of function names from the outermost caller to the innermost currently-executing function.

Write a function profiling_events(samples, n) that returns a list of events (timestamp, type, function) where type is either "START" or "END".

Behavior

When n = 1, compare consecutive stacks directly: emit END events for functions removed from the tail (innermost first), then START events for functions added to the tail (outermost first). Functions in the common prefix remain and produce no events.

When n > 1, apply per-frame debouncing. Each frame is identified by its depth and function name within a valid stack prefix. A frame becomes confirmed after appearing at the same depth with the same ancestor chain for n consecutive samples; emit its START at the timestamp of the first sample in that sequence. Similarly, a confirmed frame ends after being absent or replaced for n consecutive samples; emit its END at the timestamp when the absence began (innermost first). Shorter appearances or absences are transient noise and produce no events.

When multiple frames in a stable prefix all reach the confirmation threshold simultaneously, they all start at the same timestamp. When a parent frame ends, all its descendants must also end. At the end of input, close all remaining confirmed frames at the last timestamp seen.

Constraints

  • 1 ≤ n ≤ len(samples) ≤ 100,000
  • Timestamps are strictly increasing integers
  • Each stack contains unique, non-empty function names
  • Stack depth ≤ 1,000
  • The initial state is an empty stack

Example 1: n = 1

Samples: (10, ["A", "B"]), (20, ["A", "C"]), (30, [])

Events:
(10, START, A)
(10, START, B)
(20, END, B)
(20, START, C)
(30, END, C)
(30, END, A)

Example 2: n = 2

Samples:
(10, ["A", "B"])
(20, ["A", "C"])
(30, ["A", "C"])
(40, [])
(50, [])

Events:
(10, START, A)  — A appears for 2+ samples starting at 10
(20, START, C)  — C appears for 2 samples starting at 20
(40, END, C)    — C absent for 2 samples starting at 40
(40, END, A)    — A absent for 2 samples starting at 40

Note: B never becomes confirmed (only 1 sample)

Output Order

When multiple events share a timestamp, output END events innermost-first, followed by START events outermost-first.

Hints

Hint 1

Track how many consecutive samples each frame has been present or absent at its specific depth position

Hint 2

The longest matching prefix between consecutive stacks tells you which frames survived unchanged

Hint 3

When a parent frame's absence is confirmed, all descendant frames must also end at that same timestamp