Practice
s = "AABA" and k = 1?Solution
Step 1: Trace window expansion and frequency updates
Index 0: freq[A]=1, max_count=1, window=[0,0], max_len=1 Index 1: freq[A]=2, max_count=2, window=[0,1], max_len=2 Index 2: freq[B]=1, max_count=2, window=[0,2], window size=3, replacements needed=1 ≤ k=1, max_len=3 Index 3: freq[A]=3, max_count=3, window=[0,3], window size=4, replacements needed=1 ≤ k=1, max_len=4Step 2: Check while condition and final max_len
At index 3, window size 4 with max_count 3 means 1 replacement needed, which is allowed. So max_len updates to 4. The function returns 4.Final Answer:
Option A -> Option AQuick Check:
Window expands fully with one replacement allowed [OK]
- Off-by-one error in window size calculation
- Not updating max_count correctly
- Misunderstanding when to shrink window
def maxSatisfied(customers, grumpy, X):
n = len(customers)
base_satisfied = 0
for i in range(n):
if grumpy[i] == 0:
base_satisfied += customers[i]
window_sum = 0
for i in range(X):
if grumpy[i] == 1:
window_sum += customers[i]
max_extra = window_sum
for i in range(X, n):
if grumpy[i] == 1:
window_sum += customers[i]
if grumpy[i - X] == 0:
window_sum -= customers[i - X]
if window_sum > max_extra:
max_extra = window_sum
return base_satisfied + max_extra
Solution
Step 1: Identify subtraction condition
The code subtracts customers[i - X] only if grumpy[i - X] == 0, but it should subtract only if grumpy[i - X] == 1 because only grumpy customers were added.Step 2: Consequence of bug
This causes incorrect window sums, as some grumpy customers are never subtracted, inflating window_sum.Final Answer:
Option C -> Option CQuick Check:
Subtraction condition must match addition condition [OK]
- Confusing grumpy check in subtraction
- Forgetting to subtract outgoing element
- Mixing base satisfaction with window sums
Solution
Step 1: Understand max_count usage
max_count should track the max frequency character in the current window efficiently without recomputing entire freq array.Step 2: Identify bug in max_count update
Updating max_count by max(freq) inside the while loop is costly and can cause stale or incorrect max_count, leading to wrong window shrinking decisions.Final Answer:
Option C -> Option CQuick Check:
max_count must not be recomputed inside the shrinking loop [OK]
- Recomputing max_count on every shrink
- Not updating frequency counts correctly
- Shrinking window too early or late
n and integer k?Solution
Step 1: Identify loop behavior
The right pointer iterates over all n elements once, and the left pointer only moves forward when zeros exceed k.Step 2: Analyze queue operations and pointer moves
Each zero index is enqueued once and dequeued once, so total operations are O(n). However, each zero can cause the left pointer to move multiple times in worst case, leading to O(n * k) complexity.Final Answer:
Option A -> Option AQuick Check:
While the queue operations are O(n), the window shifts can multiply by k in worst cases [OK]
- Assuming strictly linear without considering multiple shifts
- Confusing queue size with total operations
- Ignoring worst-case scenarios
Solution
Step 1: Understand the new constraint
Characters can appear at most twice, so we must track counts, not just presence.Step 2: Modify sliding window
Use a hash map to count occurrences. Expand right pointer, increment counts. When any count > 2, move left pointer and decrement counts until valid.Step 3: Evaluate other options
Hash set only tracks presence (not counts), brute force is inefficient, DP is not straightforward here.Final Answer:
Option D -> Option DQuick Check:
Counting occurrences with sliding window handles "at most twice" constraint [OK]
- Using hash set instead of counts
- Assuming sliding window can't handle counts >1
