Practice
Solution
Step 1: Understand problem constraints
The problem requires finding the longest contiguous subarray with at most two distinct fruit types.Step 2: Identify suitable algorithm
Sliding window with a hash map efficiently tracks counts of fruit types in the current window and adjusts window size when more than two types appear, ensuring O(n) time.Final Answer:
Option D -> Option DQuick Check:
Sliding window with hashmap is classic for variable-size window problems [OK]
- Assuming greedy restart always works
- Using DP unnecessarily
- Sorting breaks contiguous constraint
nums = [4, 2, 1, 7] and k = 2, what is the value of max_sum after the loop completes?Solution
Step 1: Initialize sums
Initial window sum = nums[0] + nums[1] = 4 + 2 = 6, so max_sum = 6.Step 2: Iterate through array
i=2: current_sum = 6 + nums[2] - nums[0] = 6 + 1 - 4 = 3 (max_sum stays 6) i=3: current_sum = 3 + nums[3] - nums[1] = 3 + 7 - 2 = 8 (max_sum updated to 8)Final Answer:
Option C -> Option CQuick Check:
Max sum found is 8, which matches 8 [OK]
- Off-by-one errors in indices
- Confusing current_sum with max_sum
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
n and window size k?Solution
Step 1: Identify complexity of outer and inner loops
The algorithm iterates over each element once (O(n)). Each element is pushed and popped at most once from the deque, so total deque operations are O(n).Step 2: Check if any hidden costs exist
No nested loops or log factors; deque operations are O(1) amortized, so total time is O(n).Final Answer:
Option C -> Option CQuick Check:
Amortized deque operations ensure linear time [OK]
- Assuming nested loops cause O(n*k)
- Confusing max-heap complexity with deque
- Ignoring amortized cost of deque operations
t can be reused unlimited times (i.e., the substring in s must contain at least one occurrence of each character in t, but counts do not matter). Which modification to the sliding window algorithm correctly solves this variant?Solution
Step 1: Understand problem change
Since counts do not matter, only presence of each character is required, so frequency counts can be replaced by boolean presence tracking.Step 2: Modify sliding window logic
Incrementhavewhen a character fromtappears first time in window, decrement when it leaves. Shrink window when all characters are present.Final Answer:
Option D -> Option DQuick Check:
Tracking presence instead of counts solves the reuse variant efficiently [OK]
- Using original count-based logic
- Assuming brute force is needed
- Sorting breaks substring order
