FastPrepExclusive Function Execution Times

Exclusive Function Execution Times

Headlands Technologies logoHeadlands Technologies● MediumFULLTIMEOA
Learn

Problem statement

A single-threaded CPU runs function calls. A new call pauses the current call; when it finishes, its caller resumes. Each function has an ID from 0 through n-1, and a function may be called repeatedly or recursively.

Each record in logs has the form id:start:timestamp or id:end:timestamp. A start occurs at the beginning of its timestamp second. An end occurs after its timestamp second has finished, so that ending second counts as execution time. Logs describe complete, properly nested calls on this one CPU, in chronological order and consistent with these timing rules.

Return an integer array of length n. Entry i is the total number of seconds when function i is actually running, excluding time spent inside other calls. Combine all calls to the same ID. Functions with no calls have total 0, and time while the CPU has no active call belongs to no function. Function IDs do not encode priorities. Do not modify logs.

Function

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

Examples

Example 1

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

Function0 owns seconds0–3,6,11; function1 owns7–10; function2 owns4–5. The totals in ID order are[6,4,2].

Example 2

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

The two nested calls share ID0, so their exclusive contributions accumulate to all six occupied seconds0–5.

Example 3

n = 3logs = ["1:start:3","1:end:3","2:start:10","2:end:12"]return = [0,1,3]

Only second3 belongs to function1 and seconds10–12 belong to function2. The idle gaps contribute to no function; function0 is absent.

Constraints

  • 1 <= n <= 100 and 1 <= logs.length <= 500.
  • Every function ID is in [0,n-1]; every timestamp is an integer in [0,3000].
  • Timestamps are nondecreasing. No two start timestamps are equal, and no two end timestamps are equal.
  • All records are valid. Every start has its matching end, and each end closes the currently active call. The inclusive timing is consistent with a single thread.
  • All returned totals are at most 3001, so signed 32-bit integers suffice.

More Headlands Technologies problems

See Headlands Technologies hiring insights
public int[] getTotalExecutionTime(int n, String[] logs) {
    // write your code here
}
n3
logs["0:start:0","2:start:4","2:end:5","1:start:7","1:end:10","0:end:11"]
expected[6,4,2]
Checking account…