Practice
def totalFruit(fruits):
basket = {}
left = 0
max_length = 0
for right in range(len(fruits)):
basket[fruits[right]] = basket.get(fruits[right], 0) + 1
while len(basket) > 2:
basket[fruits[left]] -= 1
if basket[fruits[left]] == 0:
del basket[fruits[left]]
left += 1
max_length = max(max_length, right - left + 1)
return max_length
Solution
Step 1: Trace iterations for input [1, 2, 1]
Right=0: basket={1:1}, max_length=1; Right=1: basket={1:1,2:1}, max_length=2; Right=2: basket={1:2,2:1}, max_length=3.Step 2: No window shrinking needed as basket size never exceeds 2
Final max_length is 3, the length of entire array.Final Answer:
Option B -> Option BQuick Check:
All fruits fit in one window of length 3 [OK]
- Off-by-one in window length
- Shrinking window unnecessarily
- Miscounting basket size
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
n and the character set size is fixed (e.g., 26 uppercase letters)?Solution
Step 1: Analyze pointer movements
Right pointer moves from 0 to n-1 once. Left pointer only moves forward when window is invalid, also at most n times.Step 2: Consider frequency array and fixed character set
Frequency array is fixed size (26), so updates are O(1). No nested loops over n inside main loop.Step 3: Analyze space complexity
The frequency array uses O(1) space since the character set is fixed, not O(n).Final Answer:
Option A -> Option AQuick Check:
Two-pointer linear scan with fixed-size frequency array [OK]
- Assuming nested loops cause O(n^2)
- Thinking k affects inner loop complexity
- Confusing frequency map space with time complexity
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 sliding window assumptions
Sliding window relies on sums increasing as right pointer moves forward and shrinking from left to reduce sum, which requires all positive numbers.Step 2: Effect of negative numbers
Negative numbers can decrease sum inside the window unpredictably, so shrinking or expanding window does not guarantee monotonic sum changes, breaking the approach.Final Answer:
Option A -> Option AQuick Check:
Sliding window requires non-negative numbers for correctness [OK]
- Assuming sliding window works with negatives
- Thinking sorting fixes contiguous subarray sums
- Believing prefix sums binary search fails with negatives
