Trace Events from Stable Whole-Stack States
Updated
Problem statement
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:
- 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.
- For each old call left after that point, emit an
endevent, innermost first. - Then, for each new call left after that point, emit a
startevent, outermost first. - 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
nsamples 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
endevents. - 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.nmay be larger than the number of samples.- Time: timestamps are strictly increasing integers from
0to10^9. - Stack size: each stack contains at most
100frames. 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
100000characters.
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