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.)
- A. 5
- B. 3
- C. 6
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the length of the longest contiguous subarray of [0, 1, 1, 0, 1, 1, 1, 0, 0, 0] with an equal number of 0s and 1s? (Map 0 to −1…
- What is the maximum sum of a non-empty contiguous subarray of the CIRCULAR array [5, −8, 3, 4, −2, 6, −9, 7] (a subarray may wrap from the…
- What is the maximum product of a non-empty contiguous subarray of [2, −3, 0, −2, −4, 3, −1]?
- What is the next lexicographic permutation of [1, 5, 8, 4, 7, 6, 5, 3, 1]?
- Circular route: station i gives gas[i] and driving to station i + 1 costs cost[i], with gas = [3, 1, 5, 2, 6] and cost = [4, 3, 2, 4, 2]…
- A monotonic stack removes duplicate letters so that each letter appears exactly once and the result is the lexicographically SMALLEST…