FastPrepExclusive Function Execution Times

Exclusive Function Execution Times

Headlands Technologies logoHeadlands Technologies● MediumFULLTIMEOA

Updated

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]

Function 0 owns seconds 0–3,6,11; function 1 owns 7–10; function 2 owns 4–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 ID 0, so their exclusive contributions accumulate to all six occupied seconds 0–5.

Example 3

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

Only second 3 belongs to function 1 and seconds 10–12 belong to function 2. The idle gaps contribute to no function; function 0 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 interview questions

Similar questions at other companies

All Headlands Technologies interview questionsSee Headlands Technologies hiring insights
Saved
class Solution {
    public int[] getTotalExecutionTime(int n, String[] logs) {
        // write your code here
    }
}
Checking account…