Reconstruct call stacks from enter and exit traces
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"""
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
