Practice
numSubarraysWithSum([1,0,1], 2)?Solution
Step 1: Trace atMost(2)
Right=0: current_sum=1 ≤2, count=1; Right=1: current_sum=1 ≤2, count=1+2=3; Right=2: current_sum=2 ≤2, count=3+3=6Step 2: Trace atMost(1)
Right=0: current_sum=1 ≤1, count=1; Right=1: current_sum=1 ≤1, count=1+2=3; Right=2: current_sum=2 >1, shrink window: current_sum=1, left=1, count=3+2=5Final Answer:
Option D -> Option DQuick Check:
numSubarraysWithSum = 6 - 5 = 1 [BUG]
- Miscounting subarrays when shrinking window
- Off-by-one errors in count increments
- Confusing atMost(goal) with exact count
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
Solution
Step 1: Analyze loop operations
The sliding window sums are updated by adding one new element and removing one old element per iteration, so each element is processed twice.Step 2: Identify complexity
Since the loop runs n-k times and each iteration is O(1), total time is O(n). The common mistake is thinking sum is recomputed fully each time, which is O(n*k).Final Answer:
Option A -> Option AQuick Check:
Sliding window updates sum in constant time per iteration [OK]
- Assuming sum recomputed fully each window
- Confusing window size k with input size n
s2 contains a permutation of s1. Identify the line containing the subtle bug that can cause incorrect results.Solution
Step 1: Identify bug in window shrinking logic
The code decrementswindow_count[left_idx]before checking if it equalscount[left_idx]. This causes incorrect formed count updates because the check should happen before decrement.Step 2: Correct order of operations
To fix, check ifwindow_count[left_idx] == count[left_idx]before decrementingwindow_count[left_idx], then decrement and adjustformedaccordingly.Final Answer:
Option C -> Option CQuick Check:
Formed count must reflect window state before decrement [OK]
- Adjusting formed after decrementing counts
- Miscounting required matches
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
