Max Consecutive Uptime After Repairing One Window of Exactly k Log Entries

Read the full interview experience this question came from →

Quick 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.

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

A server's health log is a string of `0`s and `1`s, one character per time step: `1` means the server was up and running normally, and `0` means it had failed. You may run at most one maintenance operation, which picks a contiguous stretch of exactly `k` consecutive entries and sets every `0` in that stretch to `1` (entries that are already `1` are unchanged). Return the greatest possible length of a run of consecutive `1`s in the log after you choose the stretch optimally, or decide not to run the operation. The stretch length is fixed at exactly `k`. The operation does not let you flip up to `k` arbitrary zeros; it repairs only the zeros inside one window of `k` adjacent entries. ### Function Signature ```python def max_uptime_after_one_repair(log: str, k: int) -> int: ``` ### Rules - Positions are 0-indexed. A repair starting at index `s` must satisfy `0 <= s` and `s + k <= len(log)`, and it covers positions `s, s + 1, ..., s + k - 1`. - At most one repair is allowed. - Return one integer: the length of the longest run of consecutive `1`s in the final log. ### Constraints - `1 <= len(log) <= 10^5` - `1 <= k <= len(log)` - `log` contains only the characters `'0'` and `'1'`. ### Examples **Example 1** ```text Input: log = "0110100", k = 3 Output: 5 ``` Repairing positions 3 to 5 gives `"0111110"`, with a run of five `1`s at positions 1 to 5. **Example 2** ```text Input: log = "01010", k = 2 Output: 3 ``` The best windows are positions 1 to 2 or positions 2 to 3; both give `"01110"`. Fixing the zeros at positions 0 and 2 would give `"11110"` with a run of 4, but those two zeros do not lie inside one window of length 2, so that is not allowed. **Example 3** ```text Input: log = "0000", k = 1 Output: 1 ``` A window of length 1 repairs a single entry, producing one isolated `1`.

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

Amazon logo
Amazon
Oct 29, 2026
mediumSoftware EngineerOnline AssessmentCoding & Algorithms
0
0

A server's health log is a string of 0s and 1s, one character per time step: 1 means the server was up and running normally, and 0 means it had failed. You may run at most one maintenance operation, which picks a contiguous stretch of exactly k consecutive entries and sets every 0 in that stretch to 1 (entries that are already 1 are unchanged). Return the greatest possible length of a run of consecutive 1s in the log after you choose the stretch optimally, or decide not to run the operation.

The stretch length is fixed at exactly k. The operation does not let you flip up to k arbitrary zeros; it repairs only the zeros inside one window of k adjacent entries.

Function Signature

def max_uptime_after_one_repair(log: str, k: int) -> int:

Rules

  • Positions are 0-indexed. A repair starting at index s must satisfy 0 <= s and s + k <= len(log) , and it covers positions s, s + 1, ..., s + k - 1 .
  • At most one repair is allowed.
  • Return one integer: the length of the longest run of consecutive 1 s in the final log.

Constraints

  • 1 <= len(log) <= 10^5
  • 1 <= k <= len(log)
  • log contains only the characters '0' and '1' .

Examples

Example 1

Input:  log = "0110100", k = 3
Output: 5

Repairing positions 3 to 5 gives "0111110", with a run of five 1s at positions 1 to 5.

Example 2

Input:  log = "01010", k = 2
Output: 3

The best windows are positions 1 to 2 or positions 2 to 3; both give "01110". Fixing the zeros at positions 0 and 2 would give "11110" with a run of 4, but those two zeros do not lie inside one window of length 2, so that is not allowed.

Example 3

Input:  log = "0000", k = 1
Output: 1

A window of length 1 repairs a single entry, producing one isolated 1.