Two episodes before landing

two pointers

Two episodes before landing

Netflix Python Interview Question

Netflix's downloads feature suggests two episodes a traveler can finish before their flight lands.

Write a function closest_pair_sum(durations, limit) that takes a list of episode lengths in minutes and returns the largest total length of two different episodes that is no more than the limit. Return -1 if no two episodes fit.

Asked of

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

Example 1

Input

closest_pair_sum([30, 45, 60, 20], 80)

Output

80

Example 2

Input

closest_pair_sum([50, 60], 100)

Output

-1

Explanation

In the first example, the 60-minute and 20-minute episodes add up to exactly 80, the limit, which beats pairs such as 30 and 45, which add up to 75. In the second example, the only pair adds up to 110, which is more than 100, so the answer is -1.

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