Reconstructing function call timings from sampling profiler data
This medium-difficulty coding problem tests your ability to reconstruct precise event timings from incomplete, periodic snapshots of a running program's call stack. It appears frequently in performance-profiling roles at firms that build trading infrastructure, monitoring systems, or developer tools.
The core challenge is to infer when functions began and ended execution by comparing consecutive stack snapshots taken at irregular intervals. You must track which functions are entering (appearing for the first time in a stack) and exiting (disappearing from the stack), emit events with correct timestamps, and handle edge cases like empty inputs and programs that start or stop mid-execution. The logic hinges on careful stack comparison: diffing the previous and current stacks to determine which functions are new entries and which have returned.
- Stack-based call-tree reconstruction
- Sequence diffing and change detection
- Handling missing or incomplete state information
- Event ordering and timestamp assignment
Solutions in C++ and Python both benefit from clean abstractions—track the active stack state as you iterate through samples, emit events atomically, and test thoroughly on boundary cases (empty sample lists, single-frame programs, deep call chains).