DSA interview question
A DNA-editing tool can change up to k letters in a sequence, and researchers want the longest stretch that can be made all the same letter. A window can be made uniform when window length − (count of its most frequent letter) ≤ k, since exactly the non-majority letters need replacing. A sliding window grows to the right and shrinks from the left whenever it needs more than k replacements, tracking letter counts to find the most frequent letter.
Example: s = "AABABBA", k = 1 → 4 ("AABA" → "AAAA").
int characterReplacement(const string& s, int k); // uppercase letters A-Z
Input (s, k) | Output | Explanation |
|---|---|---|
("ABAB", 2) | 4 | Replace both As (or both Bs) |
("AABABBA", 1) | 4 | "AABA" becomes "AAAA" |
("AAAA", 0) | 4 | |
("ABCDE", 1) | 2 | |
("ABBB", 0) | 3 | |
("A", 0) | 1 |
(Confirm: replacements can turn a letter into any other letter; at most k replacements; the substring must be contiguous.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in