Find longest unique show window
Company: Netflix
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates concepts in sliding-window algorithms, hash-based duplicate tracking, case-insensitive string handling, and the extension of batch solutions to streaming (online) inputs.
Part 1: Longest Unique Show Window
Constraints
- 0 <= len(shows) <= 200000
- Each element of shows is a string
- Comparison is case-insensitive
- Use 0-based indexing
- The end index in the answer is inclusive
Examples
Input: ["Friends", "Seinfeld", "The Office", "friends", "Lost"]
Expected Output: (1, 4)
Explanation: The longest case-insensitive unique window is ["Seinfeld", "The Office", "friends", "Lost"], from index 1 to 4.
Input: ["A", "B", "a", "b"]
Expected Output: (0, 1)
Explanation: Several longest windows have length 2: [0,1], [1,2], and [2,3]. Return the earliest start index.
Input: ["A", "b", "C"]
Expected Output: (0, 2)
Explanation: All show names are distinct ignoring case, so the whole array is the answer.
Input: []
Expected Output: (-1, -1)
Explanation: Empty input has no valid window.
Input: ["Only"]
Expected Output: (0, 0)
Explanation: A single element forms a valid window of length 1.
Solution
def solution(shows):
if not shows:
return (-1, -1)
last_seen = {}
left = 0
best_start = 0
best_len = 0
for right, name in enumerate(shows):
key = name.casefold()
if key in last_seen and last_seen[key] >= left:
left = last_seen[key] + 1
last_seen[key] = right
current_len = right - left + 1
if current_len > best_len or (current_len == best_len and left < best_start):
best_len = current_len
best_start = left
return (best_start, best_start + best_len - 1)
Time complexity: O(n). Space complexity: O(min(n, m)), where m is the number of distinct show names ignoring case.
Hints
- Try using a sliding window with two pointers.
- Store the most recent index of each normalized show name so you can move the left side of the window in O(1).
Part 2: Streaming Longest Unique Show Window Reports
Constraints
- 0 <= len(shows) <= 200000
- Each element of shows is a string
- Comparison is case-insensitive
- Use 0-based indexing
- The end index in each reported tuple is inclusive
Examples
Input: ["A", "B", "a", "C"]
Expected Output: [(0, 0), (0, 1), (0, 1), (1, 3)]
Explanation: After the third show, the current unique suffix is [1,2], but the best-so-far window is still [0,1]. After the fourth show, [1,3] becomes the new best.
Input: ["Ted", "Dexter", "Office", "ted", "Lost"]
Expected Output: [(0, 0), (0, 1), (0, 2), (0, 2), (1, 4)]
Explanation: The repeat of "Ted" forces the current window to start at index 1, and adding "Lost" then creates a longer best window [1,4].
Input: ["x", "X", "x"]
Expected Output: [(0, 0), (0, 0), (0, 0)]
Explanation: All arrivals are the same show ignoring case, so the best window never exceeds length 1.
Input: []
Expected Output: []
Explanation: No arrivals means no reports.
Input: ["Solo"]
Expected Output: [(0, 0)]
Explanation: After one arrival, the longest valid window is that single element.
Solution
def solution(shows):
last_seen = {}
left = 0
best_start = 0
best_len = 0
reports = []
for right, name in enumerate(shows):
key = name.casefold()
if key in last_seen and last_seen[key] >= left:
left = last_seen[key] + 1
last_seen[key] = right
current_len = right - left + 1
if current_len > best_len:
best_len = current_len
best_start = left
reports.append((best_start, best_start + best_len - 1))
return reports
Time complexity: O(n) total time, which is O(1) average update time per arriving show. Space complexity: O(min(n, m)) extra space for tracking last-seen positions, plus O(n) space for the returned report list.
Hints
- You only need to know the current unique suffix ending at the newest item, plus the best window seen so far.
- A hash map from normalized show name to its latest index lets you update the left boundary quickly when a duplicate arrives.