KatInterview

Longest Streak of Distinct Viewing History

Arrays & HashingModel answer
MediumModel answerArrays & Hashingsliding-windowhash-maptwo-pointers

Last updated

Find the longest contiguous subsequence in a viewing history array where no show appears more than once (case-insensitive).

You're analyzing a user's streaming history represented as an array of show titles. Your task is to identify the longest contiguous segment where every show appears at most once, treating titles case-insensitively.

Input:

  • An array of strings representing show names in chronological order

Output:

  • A tuple (start, end) containing the inclusive indices of a longest non-repeating segment
  • If the input is empty, return (-1, -1)
  • When multiple segments have the same maximum length, return any one of them

Constraints:

  • Show names are compared case-insensitively ("Ozark" and "ozark" are duplicates)
  • Time complexity should be O(n) where n is the array length
  • Space complexity should be O(min(n, m)) where m is the number of unique shows

Example:

Input: ["Breaking Bad", "Ozark", "The Wire", "ozark", "Dark", "Mindhunter"]
Output: (2, 5)
Explanation: The segment ["The Wire", "ozark", "Dark", "Mindhunter"] has length 4

Follow-up: How would you adapt this solution for a streaming scenario where show names arrive one at a time, and you need to maintain the current longest non-repeating window at all times? Discuss data structures, per-element update cost, and memory characteristics.

Hints

Hint 1

Use a sliding window with two pointers that expands right and contracts left when duplicates are found

Hint 2

Track the most recent position of each normalized show name in a hash map

Hint 3

When you encounter a duplicate, be careful—its previous occurrence might already be outside your current window