Exclusive Function Execution Times
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 <= 100and1 <= 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
- Disaster RecoveryOA · Seen Nov 2026
- Array Interval SumsOA · Seen Oct 2026
- Job Runner Chain ReportsOA · Seen Oct 2026
- Minimum Changes for Distinct Adjacent CharactersOA · Seen Sep 2026
- Sliding-Window Means with IEEE Special ValuesOA · Seen Sep 2026
- As-Of JoinOA · ONSITE INTERVIEW · Seen Aug 2026
- Earliest Dependency ScheduleOA · Seen Oct 2023
- Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
- Market Order BookPHONE SCREEN · Seen Jun 2026
- Gradient Descent on a Sinusoidal ObjectivePHONE SCREEN · Seen Apr 2026
Similar questions at other companies
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Layer History with Batch Undo and RedoFigma · OA · PHONE SCREENFigma interview questions
- Asteroid CollisionMicrosoft · OAMicrosoft interview questions
- Undo/Redo Timeline State EngineKickdrum · OA
- Stack Batch RemovalIMC Trading · OAIMC Trading interview questions
- Get Function Execution TimeCapital One · OACapital One interview questions
- Compare Character Arrays with BackspacesOdoo · OAOdoo interview questions
- Adjacent Pair and Run RemovalWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Asteroid CollisionSiemens · PHONE SCREEN
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions