Practice
longestOnes([1,0,1,0], 1)?
from collections import deque
def longestOnes(nums, k):
zero_positions = deque()
left = 0
max_length = 0
for right in range(len(nums)):
if nums[right] == 0:
zero_positions.append(right)
if len(zero_positions) > k:
left = zero_positions.popleft() + 1
max_length = max(max_length, right - left + 1)
return max_length
Solution
Step 1: Trace iterations with input [1,0,1,0], k=1
Right=0: nums[0]=1, zero_positions=[], max_length=1 Right=1: nums[1]=0, zero_positions=[1], max_length=2 Right=2: nums[2]=1, zero_positions=[1], max_length=3 Right=3: nums[3]=0, zero_positions=[1,3], length=2 > k=1 triggers left update left = zero_positions.popleft()+1 = 1+1=2, zero_positions=[3], max_length remains 3Step 2: Final max_length after loop ends is 3
The longest subarray with at most 1 zero flipped is length 3.Final Answer:
Option D -> Option DQuick Check:
Manual trace confirms max_length=3 [OK]
- Off-by-one errors in window length
- Not updating left pointer correctly
- Confusing max_length update timing
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
def max_sum_subarray_sliding_window(arr, k):
n = len(arr)
window_sum = sum(arr[:k])
max_sum = 0
for i in range(k, n):
window_sum += arr[i] - arr[i - k]
if window_sum > max_sum:
max_sum = window_sum
return max_sum
Solution
Step 1: Understand initialization of max_sum
max_sum is initialized to 0, which is incorrect if all array elements are negative because the maximum sum could be less than 0.Step 2: Consequence of wrong initialization
If all sums are negative, max_sum remains 0, which is incorrect. Proper initialization should be max_sum = window_sum (sum of first k elements) or float('-inf').Final Answer:
Option A -> Option AQuick Check:
max_sum initialized to 0 causes incorrect max for negative arrays [OK]
- Initializing max_sum to zero instead of first window sum
- Off-by-one errors in loop indices
- Incorrect window_sum updates
n is the length of s and m is the length of t?Solution
Step 1: Identify main loops
The algorithm uses two pointers moving over filtered characters, each pointer advances at mostntimes.Step 2: Analyze filtering and counting
Filteringstakes O(n), sliding window over filtered characters is O(n) since each character is visited twice max.Final Answer:
Option A -> Option AQuick Check:
Two-pointer sliding window is linear time [OK]
- Confusing filtering cost
- Assuming nested loops cause O(n^2)
- Ignoring that each pointer moves only forward
p can be reused multiple times when matching substrings in s (i.e., find substrings in s that can be formed by repeating characters from p any number of times). Which of the following algorithmic changes correctly adapts the sliding window approach?Solution
Step 1: Understand reuse of characters
Characters inpcan be repeated multiple times, so window size is variable and frequency counts can exceed original counts.Step 2: Adapt sliding window logic
Use variable-size window and verify window counts do not exceed allowed multiples ofp's counts.Final Answer:
Option C -> Option CQuick Check:
Variable window and count comparison with multiples handles reuse correctly [OK]
- Keeping fixed window size
- Ignoring counts and only checking character presence
- Sorting substrings repeatedly
