Max Consecutive Uptime After Repairing One Window of Exactly k Log Entries
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: Given a server health log of 0s and 1s and a window length k, choose at most one block of exactly k consecutive entries to repair, setting every 0 inside it to 1, and report the longest stretch of consecutive 1s that can result. The problem checks linear-time string scanning and the difference between a fixed-length repair and flipping any k zeros.
Read the full Amazon Software Engineer interview experience this question came from
Constraints
- 1 <= len(log) <= 10^5
- 1 <= k <= len(log)
- log contains only the characters '0' and '1'.
- The answer is at most len(log) <= 10^5, so no value exceeds 2^31 - 1.
Examples
Input: ('0', 1)
Expected Output: 1
Explanation: Minimum valid input: a single failed entry; the only window repairs it, giving a run of 1.
Input: ('1', 1)
Expected Output: 1
Explanation: Minimum valid input: a single healthy entry; the repair changes nothing and the run is 1.
Hints
- The repaired stretch always has exactly k entries, so two zeros can be fixed together only if they fit inside one window of length k (see Example 2).
- Turning 0s into 1s never shortens a run, so running the operation is never worse than skipping it.
- For one fixed start s, think about which 1s end up in the same run as the repaired stretch.