Longest Uniform Answer Block After Limited Changes

Quick Overview

Longest Uniform Answer Block After Limited Changes. The string answerKey contains only T and F. Change at most k positions to either symbol. Return the greatest possible length of a contiguous block containing one repeated symbol after the changes.

Longest Uniform Answer Block After Limited Changes

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem The string `answerKey` contains only `T` and `F`. You may change at most `k` positions to either symbol. Return the largest length of a contiguous block that can contain one repeated symbol after those changes. ```cpp class Solution { public: int maxConsecutiveAnswers(string answerKey, int k); }; ``` ### Examples **Example 1:** ```text answerKey = "FTFTF", k = 1 Output: 3 Explanation: Changing the middle T in a length-three FTF block creates FFF; every length-four block needs two changes. ``` **Example 2:** ```text answerKey = "FFTTF", k = 2 Output: 5 Explanation: Changing both T symbols makes the entire string F. ``` ### Constraints - `1 <= answerKey.length <= 5 * 10^4`. - Characters are T or F; `1 <= k <= answerKey.length`. ### Clarifying Questions The goal is one contiguous run and the budget is at most k, not exactly k. ```hint Inspect the structure For a fixed desired symbol, how many characters in a window must change? ``` ### Reasoning checkpoints - Find the longest achievable contiguous run for either symbol while staying within the change budget. - Distinguish at most k changes from exactly k and justify that no longer feasible block is missed. ### Optional extensions Can you obtain the answer in one pass while allowing either target symbol?

Overview: Longest Uniform Answer Block After Limited Changes. The string answerKey contains only T and F. Change at most k positions to either symbol. Return the greatest possible length of a contiguous block containing one repeated symbol after the changes.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

The string answerKey contains only T and F. You may change at most k positions to either symbol. Return the largest length of a contiguous block that can contain one repeated symbol after those changes.

class Solution {
public:
    int maxConsecutiveAnswers(string answerKey, int k);
};

Examples

Example 1:

answerKey = "FTFTF", k = 1
Output: 3
Explanation: Changing the middle T in a length-three FTF block creates FFF; every length-four block needs two changes.

Example 2:

answerKey = "FFTTF", k = 2
Output: 5
Explanation: Changing both T symbols makes the entire string F.

Constraints

  • 1 <= answerKey.length <= 5 * 10^4 .
  • Characters are T or F; 1 <= k <= answerKey.length .

Clarifying Questions Guidance

The goal is one contiguous run and the budget is at most k, not exactly k.

Reasoning checkpoints

  • Find the longest achievable contiguous run for either symbol while staying within the change budget.
  • Distinguish at most k changes from exactly k and justify that no longer feasible block is missed.

Optional extensions

Can you obtain the answer in one pass while allowing either target symbol?