Exclusive Time of Functions
Updated
Problem statement
A single-threaded CPU runs n functions with ids 0 to n - 1. Functions can call other functions, and themselves. Time is split into whole units numbered 0, 1, 2, ....
"id:start:t"means a call of functionidbegins at the start of unitt. The call that was running is paused."id:end:t"means that call finishes at the end of unitt, so unittstill belongs to it. The paused caller, if any, then resumes.
The exclusive time of a function is the number of units in which one of its calls is the one actually running. Units when nothing runs belong to no function.
Return an array whose i-th value is the exclusive time of function i.
Function
exclusiveTime(n: int, logs: List<String>) → int[]Examples
Example 1
n = 2logs = ["0:start:0","1:start:3","1:end:4","0:end:7"]return = [6,2]Function 0 runs during time units 0, 1 and 2. Function 1 then runs during units 3 and 4. Function 0 continues during units 5, 6 and 7. So function 0 gets 3 + 3 = 6 units and function 1 gets 2.
Example 2
n = 1logs = ["0:start:0","0:start:2","0:end:3","0:end:5"]return = [6]Function 0 calls itself. The outer call runs during units 0 and 1, the inner call during 2 and 3, and the outer call again during 4 and 5. All six units belong to function 0, so the answer is [6].
Example 3
n = 3logs = ["0:start:0","0:end:0","2:start:1","2:end:4"]return = [1,0,4]Function 0 starts and ends in unit 0, so it gets 1 unit. Function 2 runs during units 1 to 4, which is 4 units. Function 1 never runs and gets 0.
Constraints
1 <= n <= 1002 <= logs.length <= 500- Each log is
"id:start:t"or"id:end:t"with0 <= id < nand0 <= t <= 10^9. - The logs are in time order and describe properly nested calls: every end closes the most recent call that is still open, every call is closed, a start comes later than every earlier log, and no two end logs share a timestamp.
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Fountain SafetyGoogle · ONSITE INTERVIEWGoogle interview questions
- Trace Events from Stable Whole-Stack StatesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Evaluate Ordered Action RulesStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Surviving Balls after Directional CollisionsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Render a Comment HierarchyAmazon · ONSITE INTERVIEW
- Asteroid CollisionAdobe · ONSITE INTERVIEWAdobe interview questions
- Most Frequent Call Path in Interleaved LogsRoblox · ONSITE INTERVIEWRoblox interview questions