FastPrepTrace Events from Stable Whole-Stack States
Problems/Trace Events from Stable Whole-Stack States

Trace Events from Stable Whole-Stack States

Anthropic logoAnthropic● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Anthropic full-time onsite interview. The report says the whole stack must be stable before the global trace is updated. We chose the timestamps, the event format and order, the starting state, the input limits, and what happens at the end of the input. Our version is about 85% the same as the reported question.

A sampling profiler takes snapshots of the full call stack. Turn these snapshots into function start and end events. A new stack only counts once n samples in a row show exactly the same whole stack. It is not enough for just the outer calls to match.

timestamps[i] is the time of sample i. stacks[i] lists the function names in that sample, from the outermost call to the innermost. The committed stack is the last stack that counted. It starts empty.

Keep a count of how many samples in a row have shown the same whole stack. When a different stack shows up, the count goes back to one. When the count reaches n, compare the new stack with the committed stack:

  1. Start at the outermost call and walk inward while both stacks have the same call. Stop at the first difference, or when one stack runs out.
  2. For each old call left after that point, emit an end event, innermost first.
  3. Then, for each new call left after that point, emit a start event, outermost first.
  4. The new stack becomes the committed stack.

Every event uses the timestamp of the sample that confirmed the stack, even if that stack first showed up earlier. Return each event as [eventType, timestampAsString, functionName], where the type is start or end. List the events in the order you emit them.

A few more rules:

  • A stack that never reaches n samples in a row never changes the committed stack.
  • More samples of the same stack after it is confirmed add no new events.
  • The same name at two different depths counts as two separate calls.
  • When the input ends, do not add any extra end events.
  • An empty stack clears the committed stack, but only once the empty stack 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 sample, just [main], shows up only once, so nothing is confirmed by time 2. The whole stack [main, work] then shows up twice in a row, so it is confirmed at time 3: main starts, then work starts. The single [main] sample after that does not change the committed stack. The two empty samples confirm the empty stack at time 6, so work ends and then main ends.

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 stack [a] shows up twice, so a starts at the second sample. The stack [a, b] shows up only once, so b never starts. Next, [a, c] shows up twice. Its outer call a matches the committed stack, so only c starts. Last, [a] shows up twice, so c ends and a stays open. The input then ends with no extra end events.

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"]]

Here n is 1, so every sample is confirmed right away. The first stack [f, f] has two calls named f at different depths, so f starts twice. In the next sample the outer f still matches, but the inner f is replaced by g. The inner f ends first, then g starts. The last sample is empty, so g ends and then the outer f ends.

Constraints

  • 1 <= timestamps.length = stacks.length <= 1000.
  • 1 <= n <= 1000. n may be larger than the number of samples.
  • Time: timestamps are strictly increasing integers from 0 to 10^9.
  • Stack size: each stack contains at most 100 frames. A frame is one function call.
  • Names: each function name contains 1 to 20 ASCII letters, digits, or underscores.
  • Total length: all input names together contain at most 100000 characters.

More Anthropic interview questions

  • Longest-Match Tokenizer with Unknown SymbolsONSITE INTERVIEW · Seen Oct 2026
  • Recover a Function-Call LRU Cache with a Write-Ahead LogONSITE INTERVIEW · Seen Oct 2026
  • Confirmed Profiling Events with First-Observed StartsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Convert Stack Samples to Trace EventsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Profiling Events from Consecutive Stack SamplesPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Repair the Bootloader ProgramOA · ONSITE INTERVIEW · Seen Sep 2026
  • Cache Shared Image Pipeline PrefixesONSITE INTERVIEW · Seen Aug 2026
  • Fair Coin from a Biased CoinPHONE SCREEN · Seen Nov 2026
  • Crawl Same-Hostname URLs ConcurrentlyPHONE SCREEN · Seen Oct 2026
  • Find Duplicate File GroupsPHONE SCREEN · Seen Sep 2026

Similar questions at other companies

  • Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
  • Most Frequent Call Path in Interleaved LogsRoblox · ONSITE INTERVIEWRoblox interview questions
  • Compare Character Arrays with BackspacesOdoo · OAOdoo interview questions
  • Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • Adjacent Pair and Run RemovalWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
  • Digit-Coded Card ValidationCapital One · ONSITE INTERVIEWCapital One interview questions
  • Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
  • Code Editor FoldingJane Street · ONSITE INTERVIEWJane Street interview questions
  • Path Leaf Character IteratorGoogle · ONSITE INTERVIEWGoogle interview questions
  • Hex Dump FormatterWaymo · ONSITE INTERVIEWWaymo interview questions

Anthropic interview experiences

  • Anthropic PR Review Round with Claude Code: 2 Experiences
All Anthropic interview questionsSee Anthropic hiring insights
Saved
class Solution {
    public String[][] stableTraceEvents(int[] timestamps, String[][] stacks, int n) {
        // Write your code here.
    }

}
Checking account…