Convert stack samples to execution trace
Company: Anthropic
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of sampled stack traces, event reconstruction, handling ambiguous or incomplete profiling data, and algorithmic reasoning about correctness and time/space complexity.
Constraints
- 0 <= len(samples) <= 200000
- 0 <= len(stack_i) <= 100000, and the sum of all stack lengths is at most 400000
- Timestamps must be non-decreasing; equal timestamps are allowed
- Each stack element must be a string; malformed input should raise ValueError
Examples
Input: [(0.0, ['a', 'b', 'a', 'c']), (1.0, ['a', 'a', 'b', 'c'])]
Expected Output: [(0.0, 's', 'a'), (0.0, 's', 'b'), (0.0, 's', 'a'), (0.0, 's', 'c'), (1.0, 'e', 'c'), (1.0, 'e', 'a'), (1.0, 'e', 'b'), (1.0, 's', 'a'), (1.0, 's', 'b'), (1.0, 's', 'c'), (1.0, 'e', 'c'), (1.0, 'e', 'b'), (1.0, 'e', 'a'), (1.0, 'e', 'a')]
Explanation: The common prefix is only the first 'a'. So at t=1.0, end c, a, b; then start a, b, c. After the last sample, close the remaining stack c, b, a, a at t=1.0.
Input: []
Expected Output: []
Explanation: No samples means no events.
Hints
- For two adjacent samples, the frames before the first differing depth are the only ones guaranteed to still be running.
- Emit ends before starts at each transition, and treat duplicate function names as different frames if they occur at different depths.