Compute each function's exclusive time from nested call logs

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.

Roblox logo
Roblox
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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

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

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.

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.

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