Quick 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.

Compute each function's exclusive time from nested call logs

Company: Roblox

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A single-threaded program runs `n` functions with IDs `0` to `n - 1`. You are given its call trace as a list of `logs` in chronological order. Each entry is a string in the format `"function_id:start_or_end:timestamp"`, for example `"1:start:2"` or `"1:end:5"`. A function's exclusive time is the total time spent executing that function, excluding time spent in any nested (child) function calls. Return a list of length `n` where element `i` is the exclusive time of function `i`. ### Function Signature ```python def exclusive_time(n: int, logs: list[str]) -> list[int]: ``` ### Rules - `start_or_end` is either `start` or `end`. - A `start` at timestamp `t` means the function begins at the start of time unit `t`. An `end` at timestamp `t` means the function finishes at the end of time unit `t`, so a call that starts and ends at the same timestamp runs for 1 unit. - Calls are properly nested, like a call stack, and every `start` entry has exactly one matching `end` entry. - A function may call itself. Time spent in every call of function `i` counts toward element `i`. - Only the innermost open call is executing at any moment. Time units when no call is open are not counted for any function. ### 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 ```text Input: n = 2, logs = ["0:start:0", "1:start:2", "1:end:5", "0:end:6"] Output: [3, 4] ``` Function 0 runs during units 0 and 1, function 1 runs during units 2 to 5 (4 units), and function 0 finishes with unit 6. Function 0 gets 2 + 1 = 3 units. ```text Input: n = 1, logs = ["0:start:0", "0:start:2", "0:end:5", "0:start:6", "0:end:6", "0:end:7"] Output: [8] ``` Function 0 calls itself twice. The outer call runs during units 0, 1 and 7 (3 units), the first inner call during units 2 to 5 (4 units) and the second inner call during unit 6 (1 unit), for a total of 3 + 4 + 1 = 8. ```text Input: n = 3, logs = ["0:start:0", "2:start:1", "2:end:1", "0:end:3", "1:start:5", "1:end:9"] Output: [3, 5, 1] ``` Function 0 runs during units 0, 2 and 3, and function 2 runs only during unit 1. Unit 4 is idle, then function 1 runs during units 5 to 9 (5 units).

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.

A single-threaded program runs `n` functions with IDs `0` to `n - 1`. You are given its call trace as a list of strings `logs` in chronological order. Each entry has the format `"function_id:start_or_end:timestamp"`, for example `"1:start:2"` or `"1:end:5"`. A function's exclusive time is the total time spent executing that function, excluding time spent in any nested (child) function calls. Return a list of length `n` where element `i` is the exclusive time of function `i`. ### Rules - `start_or_end` is either `start` or `end`. - A `start` at timestamp `t` means the function begins at the start of time unit `t`. An `end` at timestamp `t` means the function finishes at the end of time unit `t`, so a call that starts and ends at the same timestamp runs for 1 unit. - Calls are properly nested, like a call stack, and every `start` entry has exactly one matching `end` entry. - A function may call itself. Time spent in every call of function `i` counts toward element `i`. - Only the innermost open call is executing at any moment. Time units when no call is open are not counted for any function. Because an `end` at `t` covers all of unit `t`, a `start` that comes right after an `end` has a larger timestamp than that `end`. Two `start` entries in a row, or two `end` entries in a row, may share a timestamp; the outer call then executes for 0 units between them. ### Output Element `i` of the returned list belongs to function `i`, so the list is ordered by function ID. A function that never runs has exclusive time `0`. Only one call executes at a time, so every element is at most 10^9 + 1, which fits in a signed 32-bit integer. ### Examples Example 1: ```text Input: n = 2, logs = ["0:start:0", "1:start:2", "1:end:5", "0:end:6"] Output: [3, 4] ``` Function 0 runs during units 0 and 1, function 1 runs during units 2 to 5 (4 units), and function 0 finishes with unit 6. Function 0 gets 2 + 1 = 3 units. Example 2: ```text Input: n = 1, logs = ["0:start:0", "0:start:2", "0:end:5", "0:start:6", "0:end:6", "0:end:7"] Output: [8] ``` Function 0 calls itself twice. The outer call runs during units 0, 1 and 7 (3 units), the first inner call during units 2 to 5 (4 units) and the second inner call during unit 6 (1 unit), for a total of 3 + 4 + 1 = 8. ### 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.

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

  1. 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.
  2. Between two consecutive log entries the executing call does not change, so each gap between entries belongs to one function or to nobody.
  3. 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.

Exclusive Time of Each Function in a Call Trace

mediumRobloxSoftware Engineer

A single-threaded program runs n functions with IDs 0 to n - 1. You are given its call trace as a list of strings logs in chronological order. Each entry has the format "function_id:start_or_end:timestamp", for example "1:start:2" or "1:end:5".

A function's exclusive time is the total time spent executing that function, excluding time spent in any nested (child) function calls. Return a list of length n where element i is the exclusive time of function i.

Rules

  • start_or_end is either start or end.
  • A start at timestamp t means the function begins at the start of time unit t. An end at timestamp t means the function finishes at the end of time unit t, so a call that starts and ends at the same timestamp runs for 1 unit.
  • Calls are properly nested, like a call stack, and every start entry has exactly one matching end entry.
  • A function may call itself. Time spent in every call of function i counts toward element i.
  • Only the innermost open call is executing at any moment. Time units when no call is open are not counted for any function.

Because an end at t covers all of unit t, a start that comes right after an end has a larger timestamp than that end. Two start entries in a row, or two end entries in a row, may share a timestamp; the outer call then executes for 0 units between them.

Output

Element i of the returned list belongs to function i, so the list is ordered by function ID. A function that never runs has exclusive time 0. Only one call executes at a time, so every element is at most 10^9 + 1, which fits in a signed 32-bit integer.

Examples

Example 1:

Input: n = 2, logs = ["0:start:0", "1:start:2", "1:end:5", "0:end:6"]
Output: [3, 4]

Function 0 runs during units 0 and 1, function 1 runs during units 2 to 5 (4 units), and function 0 finishes with unit 6. Function 0 gets 2 + 1 = 3 units.

Example 2:

Input: n = 1, logs = ["0:start:0", "0:start:2", "0:end:5", "0:start:6", "0:end:6", "0:end:7"]
Output: [8]

Function 0 calls itself twice. The outer call runs during units 0, 1 and 7 (3 units), the first inner call during units 2 to 5 (4 units) and the second inner call during unit 6 (1 unit), for a total of 3 + 4 + 1 = 8.

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

Example 1
Input
(1, ['0:start:0', '0:end:0'])
Output
[1]
Notes
Minimum valid input: one call that starts and ends at timestamp 0 runs exactly 1 unit.
Example 2
Input
(2, ['0:start:0', '1:start:2', '1:end:5', '0:end:6'])
Output
[3, 4]
Notes
Source example 1: the child takes units 2 to 5 and the parent keeps units 0, 1 and 6.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def exclusive_time(n: int, logs: list[str]) -> list[int]:
Sample call
exclusive_time(1, ['0:start:0', '0:end:0'])
Language
Case 1
Input
(1, ['0:start:0', '0:end:0'])
Expected
[1]
Case 2
Input
(2, ['0:start:0', '1:start:2', '1:end:5', '0:end:6'])
Expected
[3, 4]

Browse More Questions

Show the approach

Approach

Walk the log once while keeping a stack of open calls (their function IDs) and a value prev, the first time unit that has not been charged to anyone yet. Split each entry on : so that multi-digit IDs and timestamps are read whole.

For a start at t: if a call is open, units prev to t - 1 belonged to the innermost open call, so add t - prev to its function. Then push the new call and set prev = t. For an end at t: units prev to t belonged to the innermost open call, which is the call that is ending, so add t - prev + 1 to its function, pop it, and set prev = t + 1.

Invariant: after each entry, every unit before prev has been charged to the function of the innermost call that was open during that unit, or to nobody if no call was open. A start with an empty stack charges nothing, so idle units between top-level calls are skipped. A parent's stretch closes when its child starts and reopens at the unit after the child ends, so a child's units are never charged to the parent. Charges go by the function ID on the stack, so every call of a recursive function adds to the same element.

Edge cases: a call that starts and ends at the same timestamp gets t - t + 1 = 1 unit; two starts at the same timestamp, or two ends at the same timestamp, charge 0 to the outer call; functions that never appear stay at 0; timestamps up to 10^9 keep every element at or below 10^9 + 1, inside the signed 32-bit range.

Time complexity:
O(n + m), where m = len(logs)
Space complexity:
O(n + m)