FastPrepTrace Events from Stable Whole-Stack States

Trace Events from Stable Whole-Stack States

Anthropic logoAnthropic● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

A sampling profiler records complete call stacks. Produce ordered function start and end events, but update the committed global stack only after observing n consecutive samples with exactly the same whole stack. Having the same outer prefix is not enough.

For this exercise, assume timestamps[i] is the time of sample i, and stacks[i] lists function names from outermost to innermost. The committed stack starts empty.

Count the current run of identical complete stacks. A different sample resets the run to one. On the nth sample of a run, compare its stack with the previously committed stack. Keep their longest common prefix; emit end events for the old suffix from innermost to outermost, then start events for the new suffix from outermost to innermost. Commit the new stack.

For this exercise, assume every event uses the confirmation sample timestamp, even if the candidate stack was first observed earlier. Return each event as [eventType, timestampAsString, functionName], where the type is start or end. Return events in emission order.

An unconfirmed stack never changes the committed state. Extra identical samples after confirmation emit no duplicate events. Repeated names at different depths are distinct calls. For this exercise, assume no end events are synthesized at the end of the input; an empty stack clears the committed stack only after it is itself confirmed.

Function

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

Examples

Example 1

timestamps = [1,2,3,4,5,6]stacks = [["main"],["main","work"],["main","work"],["main"],[],[]]n = 2return = [["start","3","main"],["start","3","work"],["end","6","work"],["end","6","main"]]

The first stack appears only once, so even main is not confirmed at time2. The complete stack [main, work] is confirmed at time3. A single [main] sample does not change the committed state. Two empty samples confirm the empty stack at time6.

Example 2

timestamps = [10,20,30,40,50,60,70]stacks = [["a"],["a"],["a","b"],["a","c"],["a","c"],["a"],["a"]]n = 2return = [["start","20","a"],["start","50","c"],["end","70","c"]]

The temporary b stack never confirms. The a prefix is preserved when a/c confirms. Removing c is also delayed until the complete shorter stack is observed twice.

Example 3

timestamps = [0,5,9]stacks = [["f","f"],["f","g"],[]]n = 1return = [["start","0","f"],["start","0","f"],["end","5","f"],["start","5","g"],["end","9","g"],["end","9","f"]]

With threshold1 each sample commits immediately. The two f frames are distinct calls at different depths. On replacement, inner endings precede new starts.

Constraints

  • For this exercise, assume 1 <= timestamps.length = stacks.length <= 1000.
  • For this exercise, assume 1 <= n <= 1000; it may exceed the sample count.
  • For this exercise, assume timestamps are strictly increasing integers from 0 to 10^9.
  • For this exercise, assume each stack contains at most 100 frames.
  • For this exercise, assume each function name contains 1 to 20 ASCII letters, digits, or underscores.
  • For this exercise, assume all input names together contain at most 100000 characters.

More Anthropic problems

See Anthropic hiring insights
public String[][] stableTraceEvents(int[] timestamps, String[][] stacks, int n) {
    // Write your code here.
}
timestamps[1,2,3,4,5,6]
stacks[["main"],["main","work"],["main","work"],["main"],[],[]]
n2
expected[["start", "3", "main"], ["start", "3", "work"], ["end", "6", "work"], ["end", "6", "main"]]
Checking account…