FastPrepExclusive Function Execution Time

Exclusive Function Execution Time

Decagon logoDecagon● MediumFULLTIMEPHONE SCREEN

Problem statement

A program runs on one CPU thread. Function IDs range from 0 through n - 1. A call suspends its caller until it returns, and a function may call itself or be called several times.

Each entry of logs has the form id:start:timestamp or id:end:timestamp. A start occurs at the beginning of that integer time unit; an end occurs at its end. The logs are a valid chronological call trace: an end closes the currently active call, and every call eventually ends. Idle gaps between top-level calls are allowed.

The exclusive time of a function counts the time units when that function is executing, excluding time spent in its callees. Add the contributions of every call with the same ID.

Return an array of length n containing each function's exclusive time in ID order. A function that never runs contributes 0.

Function

exclusiveTime(n: int, logs: String[]) → int[]

Examples

Example 1

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

Function 0 owns time units 0, 1 and 6, so its total is 3. Function 1 owns units 2 through 5, contributing 4.

Example 2

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

All eight occupied units, 0 through 7, belong to function 0 across its three calls. Recursive calls are charged to the same function ID.

Example 3

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

Function 1 owns only time unit 6. Function 0 owns the other seven occupied units, giving [7,1].

Constraints

  • 1 <= n <= 100.
  • 2 <= logs.length <= 500.
  • Log IDs are integers from 0 through n - 1, and timestamps are integers from 0 through 10^9.
  • Logs describe a valid single-threaded, properly nested, balanced call trace with chronological integer timestamps. No two start events share a timestamp, and no two end events share a timestamp.

More Decagon problems

See Decagon hiring insights
Saved
public int[] exclusiveTime(int n, String[] logs) {
    return new int[n];
}
Checking account…