Data Structures & AlgorithmsHardProduct Company SDE · sliding window OA
Problem: Find the length of the longest substring of 'AABABBA' that can be made all one letter after at most k = 1 replacement. Run the sliding-window code.
Problem: Find the length of the longest substring of 'AABABBA' that can be made all one letter after at most k = 1 replacement. Run the sliding-window code.
s = 'AABABBA'
k = 1
cnt = {}
l = best = mx = 0
for r, ch in enumerate(s):
cnt[ch] = cnt.get(ch, 0) + 1
mx = max(mx, cnt[ch])
while (r - l + 1) - mx > k:
cnt[s[l]] -= 1
l += 1
best = max(best, r - l + 1)
print(best)What is printed?- A. 4
- B. 3
- C. 5
- D. 6
Correct answer: A. 4
The window 'AABA' (or 'ABBA' type windows) with one replacement becomes uniform, and no window of length 5 works. The window keeps the largest same-letter count, which is the key quantity.
More Coding Interview Problems questions
- Problem: How many bits must be flipped to convert A = 29 into B = 15? Run the code. python A, B = 29, 15 print(bin(A ^ B).count('1')) What…
- Problem: What is printed by the stack-based decoder for '3[a2[c]]'? It expands k[s] to s repeated k times. python def decode(s): stack =…
- Problem: What is the minimum number of swaps needed to sort the permutation [4, 3, 2, 1] (values 1..4 each once)? Use the cycle…
- Problem: What is floor(sqrt(2000)) found by binary search? Run the code. python lo, hi = 0, 2000 while lo < hi: mid = (lo + hi + 1) // 2…
- Problem: Tasks with (duration, deadline) are (3,5), (2,4), (4,9), (1,3). Scheduling by earliest deadline first, how many tasks finish on…
- Problem: Which statement about the greedy algorithm for 'minimum number of lecture rooms' (interval partitioning) is correct?