Practice
k. You want to find the length of the longest contiguous subarray containing only 1s after flipping at most k zeros to 1s. Which algorithmic approach guarantees an optimal solution with linear time complexity?Solution
Step 1: Understand problem constraints
The problem requires the longest subarray with at most k zeros flipped to ones, which suggests a variable-size window approach.Step 2: Identify suitable algorithm
Sliding window with two pointers efficiently tracks the window boundaries and zero count, allowing linear time complexity.Final Answer:
Option A -> Option AQuick Check:
Sliding window with two pointers is the classic optimal approach for this problem [OK]
- Assuming greedy flipping without window shrink works
- Using DP unnecessarily
- Brute force is too slow
Solution
Step 1: Understand problem constraints
The problem requires finding the minimal length subarray with sum ≥ target, which suggests a variable-size window approach.Step 2: Identify algorithm that adjusts window size efficiently
Sliding window with two pointers can expand and shrink the window in O(n) time, ensuring minimal length is found without checking all subarrays.Final Answer:
Option D -> Option DQuick Check:
Sliding window is known for O(n) minimal subarray sum solutions [OK]
- Thinking sorting helps with contiguous subarrays
- Assuming DP is needed for minimal length
- Using brute force for optimality
k. What is the returned value when calling numSubarrayProductLessThanK([10, 5, 2, 6], 100)?
def numSubarrayProductLessThanK(nums, k):
if k <= 1:
return 0
product = 1.0
count = 0
left = 0
for right in range(len(nums)):
product *= nums[right]
while product >= k and left <= right:
product /= nums[left]
left += 1
count += right - left + 1
return count
Solution
Step 1: Trace product and window boundaries
Iterate right pointer: - right=0: product=10 <100, count=1 - right=1: product=50 <100, count=1+2=3 - right=2: product=100 ≥100, shrink window: product/=10=10, left=1, count=3+(2-1+1)=5 - right=3: product=10*6=60 <100, count=5+(3-1+1)=8Step 2: Final count
Total count is 8 valid subarrays.Final Answer:
Option A -> Option AQuick Check:
Count matches expected output 8 [OK]
- Off-by-one in count increment
- Not shrinking window correctly
p in s, where n = len(s) and m = len(p)?Solution
Step 1: Identify main loop operations
The window slides oversonce, so O(n) iterations.Step 2: Check frequency updates and comparisons
Frequency counts are arrays of fixed size 26, updated in O(1) time; matches count updated in O(1).Final Answer:
Option A -> Option AQuick Check:
Sliding window with count matching is linear time [OK]
- Assuming frequency map comparison is O(m)
- Confusing n*m with n
- Thinking sorting is involved
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
