Compute each function's exclusive time from nested call logs
Company: Roblox
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
Overview: Given chronological start and end logs from a single-threaded program, compute how long each function ran on its own, excluding time spent in the functions it called. It tests call stack simulation, inclusive end timestamps, recursive calls and idle gaps between calls.
Constraints
- 1 <= n <= 100
- 1 <= len(logs) <= 500
- 0 <= function_id < n
- 0 <= timestamp <= 10^9
- Logs are in chronological order.
- Every start has a matching end, and function calls are properly nested.
Examples
Input: (1, ['0:start:0', '0:end:0'])
Expected Output: [1]
Explanation: Minimum valid input: one call that starts and ends at timestamp 0 runs exactly 1 unit.
Input: (2, ['0:start:0', '1:start:2', '1:end:5', '0:end:6'])
Expected Output: [3, 4]
Explanation: Source example 1: the child takes units 2 to 5 and the parent keeps units 0, 1 and 6.
Hints
- Treat each entry as a boundary on the time line: a start at t sits just before unit t, and an end at t sits just after unit t.
- Between two consecutive log entries the executing call does not change, so each gap between entries belongs to one function or to nobody.
- Check a call that starts and ends at the same timestamp: it still runs for 1 unit, and units with no open call count for no one.