Exclusive CPU Time per Function From a Single-Threaded Profiler Trace
Last updated: November 29, 2026
Quick Overview
Given n functions and a profiler trace of entries "id:start:timestamp" and "id:end:timestamp" from a single-threaded program (end timestamps are inclusive), compute each function's exclusive time, excluding time spent in the functions it called.
0
0
0 solved
Given n functions and a profiler trace of entries "id:start:timestamp" and "id:end:timestamp" from a single-threaded program (end timestamps are inclusive), compute each function's exclusive time, excluding time spent in the functions it called.
Google engineers read flame graphs and profiles constantly, and this phone screen question asks for the core of a profiler aggregator. The interviewer checks log parsing, a call stack, and getting the inclusive-end off-by-one exactly right, including recursion.
What the Interviewer Expects
- Parses each entry into id, kind, and timestamp
- Uses a stack to track the currently running function
- Charges elapsed time to the function on top of the stack at every event
- Handles the inclusive end timestamp with a plus one
- Works for recursion and validates mismatched ends
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Report inclusive time as well as exclusive time.
- The program is multi-threaded and each entry has a thread id; what changes?
- Build the aggregated call tree (caller to callee time) like a flame graph.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
nand a list of entries like0:start:0, sorted by time. startattmeans the function begins at the start of time unitt;endattmeans it finishes at the end of unitt.- ...
High-Level Design
Keep a stack of running function ids and a pointer prev, the first time unit not yet charged to anyone. On start at t, the function on top of the stack (if any) ran from prev to t - 1, so ch...