Practice
s2 contains a permutation of s1. Given s1 = "ab" and s2 = "eidbaooo", what is the return value of the function?Solution
Step 1: Trace window expansion and counts
Fors1 = "ab", count array has counts for 'a' and 'b'. Sliding window moves overs2 = "eidbaooo". When window covers substring "ba" starting at index 3, window_count matches count, soformed == required.Step 2: Confirm return value
At this point, function returns True because a permutation ofs1is found ins2.Final Answer:
Option A -> Option AQuick Check:
Substring "ba" is a permutation of "ab" [OK]
- Off-by-one errors in window size
- Forgetting to return True when formed == required
k. The task is to count the number of contiguous subarrays where the product of all elements is less than k. Which algorithmic approach guarantees an optimal solution with linear time complexity?Solution
Step 1: Understand problem constraints
The problem requires counting subarrays with product less than k efficiently.Step 2: Identify optimal approach
Sliding window with two pointers maintains a variable-size window and updates product in O(1) per step, achieving O(n) time.Final Answer:
Option A -> Option AQuick Check:
Sliding window adjusts window size dynamically -> O(n) time [OK]
- Thinking brute force is optimal
- Confusing DP with product constraints
from collections import OrderedDict
def length_of_longest_substring_k_distinct(s: str, k: int) -> int:
if k == 0 or not s:
return 0
left = 0
max_length = 0
last_seen = OrderedDict()
for right, char in enumerate(s):
if char in last_seen:
del last_seen[char]
last_seen[char] = right
max_length = max(max_length, right - left + 1)
if len(last_seen) > k:
_, idx = last_seen.popitem(last=False)
left = idx + 1
return max_length
Solution
Step 1: Locate max_length update position
max_length is updated before shrinking the window when distinct chars exceed k, so invalid windows are counted.Step 2: Understand impact
This causes max_length to include substrings with more than k distinct characters, violating constraints.Final Answer:
Option C -> Option CQuick Check:
Moving max_length update after shrinking fixes correctness [OK]
- Updating max_length too early before shrinking window
- Confusing order of operations in sliding window
Solution
Step 1: Understand window size meaning
The window represents the subarray to exclude, which must be length n - k, not k.Step 2: Identify impact of wrong window size
Using k as window size causes the algorithm to exclude wrong subarray length, leading to incorrect results.Final Answer:
Option D -> Option DQuick Check:
Correct window size is n - k for the sliding window [OK]
- Confusing window size with k instead of n-k
- Incorrect loop boundaries
- Wrong comparison operator in min update
Solution
Step 1: Identify iteration cost
The algorithm iterates over the array once with right pointer, and left pointer only moves forward, so each element is visited at most twice, resulting in O(n) time.Step 2: Consider map operations and space
Map lookups and updates are O(1) average. Space used is O(n) for last_seen map storing indices.Final Answer:
Option C -> Option CQuick Check:
Each element visited at most twice by pointers -> O(n) time [OK]
- Confusing map operations as O(log n)
- Assuming nested loops cause O(n²)
