Data Structures & AlgorithmsVery hardProduct company OA

With at most k = 2 character replacements, what is the length of the longest substring of 'AABCABBB' that can be turned into a single repeated letter? (Sliding window: window length − count of its most frequent letter ≤ k.)

  1. A. 5
  2. B. 3
  3. C. 6
  4. D. 2

Correct answer: C. 6

'BCABBB' (indices 2–7) has 4 B's and 2 others → replace C and A → 'BBBBBB', length 6. Trying only the letter A gives 'AABCA' = 5; 3 is the longest run with no replacements (BBB) and 2 the longest run of A.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic