Merge viewing sessions

arrays

Merge viewing sessions

Netflix Python Interview Question

Netflix's playback team records each viewing session as a start and end minute. Sessions that overlap or touch should count as one continuous session.

Write a function merge_sessions(sessions) that takes a list of [start, end] pairs in any order and returns the merged sessions as a list of [start, end] pairs, sorted by start. Two sessions touch when one ends at the same minute the next one starts.

Asked of

  • Data Engineer
  • Data Scientist
  • Analytics Engineer
  • ML Engineer
  • AI Engineer

Example 1

Input

merge_sessions([[10, 20], [15, 30], [40, 50]])

Output

[[10, 30], [40, 50]]

Example 2

Input

merge_sessions([[5, 10], [10, 12]])

Output

[[5, 12]]

Explanation

In the first example, the session from 10 to 20 overlaps the one from 15 to 30, so they become one session from 10 to 30. The session from 40 to 50 does not touch either of them, so it stays on its own.

Submit also runs 3 hidden test cases that check edge cases.