The inclusive part is easy, you just grab the start and end timestamps for the target.
Use a stack to track active function calls, pushing on start and popping on end. For the target function, compute inclusive time as end - start, and exclusive time by subtracting the durations of direct child calls (tracked via the stack).
Pro tip: Clarify assumptions about log validity and nesting, and discuss handling edge cases like recursive calls or multiple instances of the target function.
Ensure logs are sorted by timestamp and properly nested. Handle any malformed entries.
Iterate through logs, using a stack to track active functions. On 'start', push; on 'end', pop and compute duration.
For each instance of the target function, inclusive time = end_timestamp - start_timestamp. Sum if multiple instances.
While the target is on the stack, track time spent in direct child calls (functions called directly by target). Subtract this from inclusive time.
Consider recursive calls, multiple target instances, and overlapping or invalid logs. Discuss with interviewer.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.