Frontend School

Calculate Playback Duration within Window

DSA
medium

Calculate Playback Duration within Window

Medium
Intervals
Sorting

Overview

At Netflix, optimizing content delivery and user experience is paramount. Imagine a scenario where a user has a curated list of content segments (e.g., scenes from a show, trailers, bonus features) they wish to watch. To provide a smooth experience, we need to calculate the actual unique viewing time within a specific, predefined playback window, accounting for potential overlaps between content segments and segments extending beyond the window.

What you need to do / Task

Your task is to implement a function that takes a list of content segments and a single playback window. It should return the total unique duration of content that falls within the specified playback window. You must account for segments that might overlap with each other or extend partially/fully outside the given window.

Input & output

  • contentSegments: An array of intervals, where each interval [start, end] represents a content segment. start and end are non-negative integers representing time points. The intervals are inclusive of start and exclusive of end (i.e., [start, end)).
  • playbackWindow: A single interval [windowStart, windowEnd] representing the total time available for playback. windowStart and windowEnd are non-negative integers, inclusive of windowStart and exclusive of windowEnd.
  • Return: A number representing the total unique duration of content segments that fall within the playbackWindow.

Clarifications

  • All time values (start, end, windowStart, windowEnd) are non-negative integers.
  • An interval [a, b) implies a is included, b is excluded. The duration of [a, b) is b - a.
  • If contentSegments is empty or playbackWindow has zero or negative duration (windowStart >= windowEnd), the total duration should be 0.
  • The input contentSegments are not necessarily sorted.

Examples

1

Example 1

Input:

contentSegments = [[1, 5], [3, 7], [8, 10]], playbackWindow = [0, 10]

Output:

8

Note:

All segments are within the window. Merging [1,5] and [3,7] yields [1,7]. The effective segments are [[1,7], [8,10]]. Total duration is (7-1) + (10-8) = 6 + 2 = 8.

2

Example 2

Input:

contentSegments = [[0, 3], [5, 8], [7, 12]], playbackWindow = [2, 10]

Output:

6

Note:

  1. Clip [0,3] to [2,10] -> [2,3]. 2. Clip [5,8] to [2,10] -> [5,8]. 3. Clip [7,12] to [2,10] -> [7,10]. Resulting clipped segments: [[2,3], [5,8], [7,10]]. Merging [5,8] and [7,10] yields [5,10]. The effective segments are [[2,3], [5,10]]. Total duration is (3-2) + (10-5) = 1 + 5 = 6.
3

Example 3

Input:

contentSegments = [[10, 20], [25, 30]], playbackWindow = [0, 5]

Output:

0

Note:

All contentSegments are entirely outside the playbackWindow. After clipping, there are no valid segments remaining. Total duration is 0.

Constraints

Constraints & assumptions

  • 0 <= contentSegments.length <= 10^5
  • 0 <= start <= end <= 10^9 for any [start, end] in contentSegments.
  • 0 <= windowStart <= windowEnd <= 10^9 for playbackWindow = [windowStart, windowEnd].
  • The total unique duration will fit within a standard 64-bit integer type (e.g., JavaScript's number).

What interviewers evaluate

  • Correctness: The solution correctly calculates the unique playback duration under various scenarios, including complex overlaps and edge cases.
  • Efficiency: The algorithm demonstrates optimal time and space complexity for the given constraints.
  • Clarity and Structure: The code is well-organized, readable, and easy to understand.
  • Robustness: The solution handles edge cases such as empty inputs, segments entirely outside the window, or zero-duration intervals appropriately.

Loading editor...