FastPrepConfirmed Profiling Events with First-Observed Starts
Problem · Array

Confirmed Profiling Events with First-Observed Starts

Learn this problem
HardAnthropic logoAnthropicFULLTIMEPHONE SCREEN

Problem statement

Related interpretation: Profiling Events from Consecutive Stack Samples uses the confirmation sample itself as the start timestamp.

A sampling profiler records the active call stack at strictly increasing timestamps. You are given parallel arrays:

  • timestamps[i] is the time of sample i.
  • stacks[i] lists active function names from outermost to innermost.

A call is identified by its full stack prefix, so recursive calls with the same function name at different depths are distinct calls.

Confirmation and Start Times

A call's current run begins at the first sample in which its exact stack prefix appears. The run continues only while that prefix appears in every consecutive sample.

A call becomes confirmed when its current run reaches n consecutive samples. Emit its start event only when this confirmation occurs, but use the timestamp of the first sample in that run: ["start", firstObservedTimestamp, functionName].

If a call disappears before reaching n samples, it emits no events. If the same prefix appears again later, that appearance begins a new run.

End Times and Event Order

If a confirmed call is absent from a later sample, emit ["end", currentTimestamp, functionName]. At each processed sample, emit endings from innermost to outermost before emitting newly confirmed starts from outermost to innermost.

Return events in emission order; do not sort them afterward by their stored timestamps. Do not synthesize end events after the final sample, so calls in the final sampled stack remain active.

Output Format

Return all events as strings. Convert each integer timestamp to its decimal string representation.

Function

generateProfilingEvents(timestamps: int[], stacks: String[][], n: int) → String[][]

Examples

Example 1

timestamps = [10,20,30,40,50]stacks = [["main"],["main","parse"],["main","parse"],["main"],["main"]]n = 2return = [["start","10","main"],["start","20","parse"],["end","40","parse"]]

main becomes confirmed at timestamp 20, but its run began at 10, so its start event stores 10. parse becomes confirmed at 30 and stores its first-observed time, 20. It ends when absent at 40. No final end is synthesized for main.

Example 2

timestamps = [1,2,3,4,5,6]stacks = [["a"],["a","a"],["a","a"],["a","tmp"],["a","b"],["a","b"]]n = 2return = [["start","1","a"],["start","2","a"],["end","4","a"],["start","5","b"]]

The outer and recursive a calls confirm separately, with first-observed start times 1 and 2. The recursive call ends at 4. tmp appears only once and is suppressed, while b confirms at 6 but stores its first-observed time, 5.

Constraints

  • 1 <= timestamps.length == stacks.length <= 1000
  • 1 <= n <= timestamps.length
  • Timestamps are strictly increasing signed 32-bit integers.
  • 0 <= stacks[i].length <= 100
  • Every function name is non-empty.

More Anthropic problems

drafts saved locally
public String[][] generateProfilingEvents(int[] timestamps, String[][] stacks, int n) {
    // Write your code here.
}
timestamps[10,20,30,40,50]
stacks[["main"],["main","parse"],["main","parse"],["main"],["main"]]
n2
expected[["start", "10", "main"], ["start", "20", "parse"], ["end", "40", "parse"]]
checking account