Practice
numSubarraysWithSum([1,0,1], 2)?Solution
Step 1: Trace atMost(2)
Right=0: current_sum=1 ≤2, count=1; Right=1: current_sum=1 ≤2, count=1+2=3; Right=2: current_sum=2 ≤2, count=3+3=6Step 2: Trace atMost(1)
Right=0: current_sum=1 ≤1, count=1; Right=1: current_sum=1 ≤1, count=1+2=3; Right=2: current_sum=2 >1, shrink window: current_sum=1, left=1, count=3+2=5Final Answer:
Option D -> Option DQuick Check:
numSubarraysWithSum = 6 - 5 = 1 [BUG]
- Miscounting subarrays when shrinking window
- Off-by-one errors in count increments
- Confusing atMost(goal) with exact count
Solution
Step 1: Analyze outer and inner loops
The right pointer moves from 0 to n-1 once; the left pointer only moves forward when shrinking window.Step 2: Each element is added and removed at most once
Thus total operations are proportional to 2n, which is O(n).Final Answer:
Option A -> Option AQuick Check:
Two-pointer sliding window with hashmap is linear time [OK]
- Assuming nested loops multiply to n²
- Thinking hashmap operations add log n
- Confusing distinct fruit count with input size
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: Analyze pointer movements
Right pointer moves forward once per character. Left pointer only moves forward when duplicates are found, never backward. Each character is visited at most twice.Step 2: Consider hash map operations
Hash map lookups and updates are O(1) average. Thus total time is O(n).Final Answer:
Option A -> Option AQuick Check:
Two-pointer sliding window with hash map -> linear time [OK]
- Assuming nested loops cause O(n^2)
- Confusing hash map average with worst-case
Solution
Step 1: Identify odd number condition with negatives
Negative odd numbers still have odd absolute value, so check must consider absolute value.Step 2: Modify odd check
Change condition to nums[right] % 2 != 0 or abs(nums[right]) % 2 == 1 to correctly count odd numbers.Final Answer:
Option A -> Option AQuick Check:
Oddness depends on absolute value, not sign [OK]
- Assuming negative numbers are never odd
- Resetting sliding window unnecessarily
- Switching to prefix sums without need
