← Questions

Reconstruct call stacks from enter and exit traces

0
AnthropicCodingLast asked Sep 2026

A profiler gives you call stacks sampled over time, outermost frame first. Turn them into function start and end events, then go the other way: replay start/end events into stacks and the time spent on each. Follow-ups cover noisy samples (ignore frames seen in fewer than k consecutive samples), recursion, malformed traces, and traces where only recent frames are visible.

def events_from_samples(samples, min_samples=1):
    """samples: [(timestamp, ["main", "f", ...])] -> [("start" | "end", timestamp, name)]"""


def folded_stacks(events):
    """events -> {"main;f": time spent with exactly that stack}; raise ValueError on a bad trace"""
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding