Solo Stream Exposure
Duration each interval is active without overlap from other intervals
A music streaming service is reviewing playback sessions from one shared account. Several sessions may be running at the same time on different devices.
You are given sessions, where sessions[i] = [start, end] describes session i. A session is active during the half-open interval [start, end): it is active at start, but not at end. Timestamps are integer offsets from the beginning of the audit and may be negative.
A session receives solo exposure only while it is the account's sole active session. Any time with two or more active sessions contributes to none of them.
Return an integer array of length sessions.length. Its element at index i must be the total duration of solo exposure received by session i.
Sessions are distinct records even if their intervals are identical. The input is not necessarily sorted, and the result must use the original input order.
Examples
Example 1
Input: sessions = [[0,8],[3,5],[6,10]] Output: [4,0,2]
Session 0 is alone from time 0 to 3 and from 5 to 6. Session 1 is never alone. Session 2 is alone from 8 to 10. The periods with overlapping sessions do not earn exposure.
Example 2
Input: sessions = [[0,10],[2,8],[2,8]] Output: [4,0,0]
The outer session is alone before the two identical inner sessions begin and after they end. The inner sessions are distinct, so neither earns solo exposure.
Example 3
Input: sessions = [[5,9],[-3,0],[0,5]] Output: [4,3,5]
The sessions meet at their boundaries without overlapping. Because ends are excluded, each session earns its entire duration.
Constraints
- 0 <= sessions.length <= 4000
- Each element of sessions contains exactly two integers: start and end.
- -10^9 <= start < end <= 10^9
The intended solution runs in O(n log n) time and uses O(n) auxiliary space. Integer arithmetic suffices; touching boundaries have zero duration and do not overlap.
Hints
Show hint 1Hint 1
The set of active sessions can change only at a start or end timestamp.
Show hint 2Hint 2
Sort endpoint events and process all events at the same timestamp together. Between consecutive timestamps, credit the span only if exactly one session is active.
Follow-up questions
What an interviewer might ask once you have a working solution.
- How would you also return the total duration during which at least two sessions are active?
- How would you return each session's maximal solo-exposure intervals instead of only its total duration?
Practice this with an AI interviewer
Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.
Start this problem