Get Function Execution Time
Updated
Problem statement
When multiple tasks are included in a single thread/CPU, the tasks are scheduled based on the principle of preemption. When a higher priority task arrives, the currently executing task with lower priority gets preempted, i.e., it becomes idle until the higher priority task is complete.
There are 3 functions to be executed in a single thread with IDs between 0 and n-1. Given an integer n representing the number of functions to be executed, and an array logs containing the timestamp and function ID, determine the exclusive time of each of the functions. It is noted that the total of execution time should be divided among functions according to their representing an integer log in the form {function_id, "start"|"end", timestamp} indicating when a function started or ended at a time identified by the timestamp value.
Note: While calculating the execution time of a function both the starting and ending times of the function calls are to be included. The log of the function calls is sorted by timestamp. When a function is preempted at the beginning of timestamp second, the log of this time stamp is {"preempting_id", "start", timestamp}. When a function function_id is preempted after completing timestamp second, the log record is {"after_timestamp_record"}.
Function
getFunctionExecutionTime(n: int, logs: String[]) → int[]
Complete the function getFunctionExecutionTime in the editor.
getFunctionExecutionTime has the following parameters:
- 1.
int n: the number of functions to be executed - 2.
String logs[m]: the execution logs of the different calls to the functions
Returns
int[n]: the execution time of all functions with IDs [0,n-1]
💐, Aura Man with the hard carry!༊·°🌺𓍼ོ
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]
Suppose n = 3, logs = ["0:start:0", "2:start:4", "2:end:5", "1:start:7", "1:end:10", "0:end:11"] Thus the total number of seconds allocated to functions 0, 1, and 2 is 6, 4, and 2 respectively. Hence the answer is [6, 4, 2].
Constraints
- 1 ≤ n ≤ 100
- 1 ≤ m ≤ 500
- 0 ≤ function_id < n
- 0 ≤ timestamp ≤ 3*10^3
- The timestamps are given in non-decreasing order.
- No two starting timestamps and no two ending timestamps are equal
- Every function "start" call has a corresponding "end" call
More Capital One interview questions
- Count Pairs of Overlapping SquaresOA · Seen Nov 2026
- Distinct Values on Maximum-Sum FramesOA · Seen Nov 2026
- Simulate Orchard Rot SpreadOA · Seen Nov 2026
- Lowest Number in an Open RangeOA · Seen Nov 2026
- Branch with the Most FilesOA · Seen Nov 2026
- Earliest Meeting Time for All EmployeesOA · Seen Nov 2026
- Earn the Most 🐝OA · Seen Nov 2026
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Rank Football TeamsOA · Seen Oct 2026
- Count Subarrays Matching a Comparison PatternOA · Seen Oct 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
- Exclusive Function Execution TimesHeadlands Technologies · OAHeadlands Technologies 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
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences