Amazon OA Interview Question: Minimum Watch Score of a Contiguous Period

15 Views
No Comments

Given an array watch_history of size n, that represents the web series watched by a viewer over a period of days, and two integers, series1 and series2, report the minimum watch score of a contiguous period of days in which both series1 and series2 have been viewed at least once.

If series1 and series2 are the same value, one occurrence during the period is sufficient.

The watch score of a contiguous period is the number of distinct series watched during that period.

Return the minimum watch score.

Function Description

Complete the function getMinScore in the editor below.

getMinScore has the following parameters:

  • INTEGER_ARRAY watch_history
  • INTEGER series1
  • INTEGER series2

Returns

int: the minimum watch score of a contiguous period of days in which series1 and series2 have been viewed at least once

Example

n = 5
series1 = 1
series2 = 2
watch_history = [1, 3, 2, 1, 4]

The contiguous periods of days in which series1 and series2 have been viewed at least once are:

  • [1, 3, 2] → watch score 3
  • [3, 2, 1] → watch score 3
  • [2, 1] → watch score 2
  • [1, 3, 2, 1] → watch score 3
  • [1, 3, 2, 1, 4] → watch score 4

The minimum watch score is 2.

Constraints

  • 1 <= n <= 10^5
  • 1 <= watch_history[i] <= 10^9
  • 1 <= series1, series2 <= 10^9
  • series1 and series2 are not necessarily distinct
  • There will be at least one occurrence of both series1 and series2 in watch_history

This problem is a sliding-window optimization task: find the smallest contiguous subarray that contains both target series and has the minimum number of distinct values, which is the watch score. A standard approach is to expand a window with two pointers while tracking element counts in a hash map, then shrink the left side whenever the window already satisfies the requirement. If series1 and series2 are the same, only one occurrence is needed. With n up to 10^5, an O(n) window-based solution is the natural fit.

END
 0