Exclusive Function Execution Time
Updated
Problem statement
A program runs on one CPU thread. Function IDs range from 0 through n - 1. A call suspends its caller until it returns, and a function may call itself or be called several times.
Each entry of logs has the form id:start:timestamp or id:end:timestamp. A start occurs at the beginning of that integer time unit; an end occurs at its end. The logs are a valid chronological call trace: an end closes the currently active call, and every call eventually ends. Idle gaps between top-level calls are allowed.
The exclusive time of a function counts the time units when that function is executing, excluding time spent in its callees. Add the contributions of every call with the same ID.
Return an array of length n containing each function's exclusive time in ID order. A function that never runs contributes 0.
Function
exclusiveTime(n: int, logs: List<String>) → int[]Examples
Example 1
n = 2logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]return = [3,4]Function 0 owns time units 0, 1 and 6, so its total is 3. Function 1 owns units 2 through 5, contributing 4.
Example 2
n = 1logs = ["0:start:0","0:start:2","0:end:5","0:start:6","0:end:6","0:end:7"]return = [8]All eight occupied units, 0 through 7, belong to function 0 across its three calls. Recursive calls are charged to the same function ID.
Example 3
n = 2logs = ["0:start:0","0:start:2","0:end:5","1:start:6","1:end:6","0:end:7"]return = [7,1]Function 1 owns only time unit 6. Function 0 owns the other seven occupied units, giving [7,1].
Constraints
1 <= n <= 100.2 <= logs.length <= 500.- Log IDs are integers from
0throughn - 1, and timestamps are integers from0through10^9. - Logs describe a valid single-threaded, properly nested, balanced call trace with chronological integer timestamps. No two start events share a timestamp, and no two end events share a timestamp.
More Decagon Phone Screen questions
- Count Complete Tic-Tac-Toe GamesPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Rolling CSAT TrackerPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Batch API Requests with Retry DelaysPHONE SCREEN · Seen Sep 2026
- Recipe Cart with Undo and RedoPHONE SCREEN · Seen Sep 2026
- Recipe Shopping Cart DiscountsPHONE SCREEN · Seen Aug 2026
- Versioned Recipe Shopping CartPHONE SCREEN · Seen Jul 2026
- Nested Recipe Cart TransactionsPHONE SCREEN · Seen Jul 2026
- Count Hits in the Last Five MinutesPHONE SCREEN · Seen Apr 2026
- Largest Rectangle in a HistogramPHONE SCREEN · Seen Dec 2024
Similar questions at other companies
- Asteroid CollisionSiemens · PHONE SCREEN
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Daily TemperaturesCARS24 · PHONE SCREEN
- Next Greater Indices and Maximum GapTikTok · PHONE SCREENTikTok interview questions
- Next Greater Value in a Circular ArrayPayPay · PHONE SCREENPayPay interview questions
- Stack with Constant-Time Middle QueriesAmazon · PHONE SCREEN
- Generic Stack Operation SequenceArista Networks · PHONE SCREENArista Networks interview questions
- Stock Span IFreshworks · PHONE SCREENFreshworks interview questions
- Consecutive Smaller Run LengthBloomberg LP · PHONE SCREENBloomberg LP interview questions