Find Longest Activatable Server Streak
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Take-home Project
Quick Answer: This question evaluates array algorithm skills, specifically recognition of sliding-window or two-pointer techniques and reasoning about time and space efficiency for contiguous subarray problems.
Constraints
- 0 <= len(serverStates) <= 200000
- serverStates[i] is either 0 or 1
- 0 <= k <= len(serverStates)
Examples
Input: ([1, 0, 0, 1, 1, 0, 1], 2)
Expected Output: 5
Explanation: By turning on two off servers, the longest contiguous all-on segment has length 5.
Input: ([1, 1, 1, 1], 1)
Expected Output: 4
Explanation: All servers are already on, so the full array is the answer.
Input: ([], 3)
Expected Output: 0
Explanation: There are no servers, so the longest streak is 0.
Input: ([0, 0, 1, 1, 0, 1, 1, 1, 0], 0)
Expected Output: 3
Explanation: No changes are allowed, so we take the longest existing run of ones, which is length 3.
Input: ([0, 0, 0], 5)
Expected Output: 3
Explanation: You can turn on all off servers, so the whole array becomes one contiguous streak of length 3.
Input: ([0], 1)
Expected Output: 1
Explanation: Turning on the single server creates a streak of length 1.