Frontend School

Premium Playback Time

DSA
medium

Premium Playback Time

Medium
Intervals
Arrays
Sorting

Overview

Spotify is constantly evaluating the impact of its premium features. One key metric is understanding how much time users spend engaging with premium content. This problem asks you to calculate the total duration a user spent listening during designated "premium content" windows, based on their listening history.

What you need to do / Task

You are given two lists of time intervals:

  1. userSessions: Represents a user's listening history. Each interval [start, end] indicates a period when the user was actively listening. These intervals are guaranteed to be sorted by start time and non-overlapping.
  2. premiumContentWindows: Represents periods when special premium content (e.g., ad-free listening, exclusive commentary) was available. These intervals may be overlapping and are not necessarily sorted.

Your task is to implement a function calculatePremiumPlaybackTime that returns the total duration (in minutes) the user spent listening within any premium content window.

Input & output

  • Input:
    • userSessions: An array of arrays of numbers, number[][]. Each inner array [start, end] represents a listening session. 0 <= start < end. Intervals are sorted by start time and are non-overlapping.
    • premiumContentWindows: An array of arrays of numbers, number[][]. Each inner array [start, end] represents a premium content window. 0 <= start < end. These intervals may be overlapping and are not necessarily sorted.
  • Output:
    • A single number, number, representing the total duration (sum of lengths of intersections) the user spent listening within premium content windows.

Clarifications

  • All times are non-negative integers.
  • An interval [start, end] includes start and excludes end. The length is end - start.
  • If userSessions or premiumContentWindows are empty, the total premium playback time is 0.
  • The problem asks for the total duration of user sessions that fall within any premium content window. This means if a user session [10, 20] overlaps with two distinct premium windows [5, 15] and [18, 25], the total counted time would be (15-10) + (20-18) = 5 + 2 = 7 (assuming premiumContentWindows were merged into [[5,15], [18,25]]). This implies you should first merge premiumContentWindows to simplify the intersection logic.

Examples

1

Example 1

Input:

userSessions = [[0, 5], [10, 15], [20, 25]], premiumContentWindows = [[3, 7], [12, 18]]

Output:

5

Note:

Merged premium windows: [[3, 7], [12, 18]].

  • Session [0, 5] overlaps with [3, 7] for [3, 5], length 2.
  • Session [10, 15] overlaps with [12, 18] for [12, 15], length 3.
  • Session [20, 25] has no overlap. Total: 2 + 3 = 5.
2

Example 2

Input:

userSessions = [[1, 10]], premiumContentWindows = [[0, 3], [5, 8], [7, 12]]

Output:

7

Note:

Merged premium windows: [[0, 3], [5, 12]].

  • Session [1, 10] overlaps with [0, 3] for [1, 3], length 2.
  • Session [1, 10] overlaps with [5, 12] for [5, 10], length 5. Total: 2 + 5 = 7.
3

Example 3

Input:

userSessions = [[1, 5]], premiumContentWindows = [[6, 10]]

Output:

0

Note:

Merged premium windows: [[6, 10]].

  • Session [1, 5] has no overlap with [6, 10]. Total: 0.

Constraints

Constraints & assumptions

  • 0 <= userSessions.length <= 10^4
  • 0 <= premiumContentWindows.length <= 10^4
  • userSessions[i].length == 2, premiumContentWindows[i].length == 2
  • 0 <= start < end <= 10^9 for all intervals.
  • userSessions are sorted by start time and non-overlapping.
  • premiumContentWindows may be unsorted and overlapping.

Out of scope

  • Handling negative time values or invalid interval formats (e.g., start >= end). Assume valid input as per constraints.
  • Optimizing for extremely sparse or dense interval sets beyond standard O(N log N) or O(N + M) approaches.

What interviewers evaluate

  • Correctness and Robustness: The solution accurately calculates the total premium playback time for all valid inputs, including edge cases (empty lists, no overlap, full containment, partial overlap).
  • Algorithm Efficiency: The chosen approach demonstrates an understanding of time and space complexity, particularly for interval manipulation. Aim for optimal or near-optimal performance.
  • Code Structure and Readability: The code is well-organized, easy to understand, and follows common software engineering practices (e.g., meaningful variable names, clear helper functions if applicable).
  • Problem Decomposition: The candidate effectively breaks down the problem into manageable sub-problems (e.g., merging intervals, finding intersections).

Loading editor...