FastPrepProfiling Events from Consecutive Stack Samples
Problem · Array

Profiling Events from Consecutive Stack Samples

Learn this problem
HardAnthropic logoAnthropicFULLTIMEPHONE SCREEN

Problem statement

Related interpretation: Confirmed Profiling Events with First-Observed Starts uses the same confirmation rule but stores the first observation in the successful run 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 Rule

A call becomes confirmed after its exact stack prefix appears in n consecutive samples. When it first becomes confirmed, emit ["start", confirmationTimestamp, functionName], using the timestamp of the nth consecutive sample.

If a confirmed call is absent from a later sample, emit ["end", currentTimestamp, functionName]. At one timestamp, emit endings from innermost to outermost before emitting newly confirmed starts from outermost to innermost. A call that disappears before reaching n samples emits no events.

Do not synthesize end events after the final sample; calls in the final sampled stack remain active.

Output Format

Return all events in order 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","20","main"],["start","30","parse"],["end","40","parse"]]

main is confirmed at timestamp 20. parse is confirmed at 30 and ends when it is 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","2","a"],["start","3","a"],["end","4","a"],["start","6","b"]]

The outer and recursive a calls confirm separately at timestamps 2 and 3. The recursive call ends at 4. tmp appears only once and is suppressed, while b confirms at 6.

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", "20", "main"], ["start", "30", "parse"], ["end", "40", "parse"]]
checking account