Cosmic Module
D
Qubits of DPK
March 15, 2026
Core DSA
1️⃣ LC #3 — Longest Substring Without Repeating Characters
(Also appears in Pattern 2 Hard — see that page for full details)
Core Idea
Sliding window with HashSet. Expand right always. When duplicate, shrink from left.
Optimized → O(n) Time | O(min(n,alphabet)) Space:
java
QUBITS OF DPK
Dry Run
Input: s = "abcabcbb"
Output: 3
Key Tricks
- Window = right - left + 1
- Shrink until duplicate removed, then add new char
- HashMap optimization: jump left directly to lastSeen + 1
30-Second Interview Explanation
Sliding window. Expand right always. When duplicate found in window, shrink from left until removed. Track max window size. Time O(n), Space O(alphabet).
️ Interview Traps
- Off-by-one: right - left + 1 not right - left
Most Common Follow-ups
Q1: Optimize shrinking with HashMap?
java
QUBITS OF DPK
Jump left directly, avoiding repeated shrinking. Still O(n).
2️⃣ LC #424 — Longest Repeating Character Replacement
Core Idea
Sliding window. Window is valid if (windowSize - maxFreq) <= k. Expand right, shrink when invalid.
Optimized → O(n) Time | O(1) Space:
java
QUBITS OF DPK
Dry Run
Input: s = "AABABBA", k=1
Output: 4
Key Tricks
- Valid window: windowSize - maxFreq <= k (non-dominant chars <= replacements)
- maxFreq never decreases — only matters if larger window is valid
- Don't recompute maxFreq when shrinking — stale maxFreq is fine
30-Second Interview Explanation
Window valid when (size - mostFreqChar) <= k. Expand right. When window needs more than k replacements, shrink left. Track max valid window. Time O(n), Space O(1).
️ Interview Traps
- Recomputing maxFreq during shrink — unnecessary and makes it O(26n)
- Not understanding why stale maxFreq is correct: we only care if a LARGER window becomes valid
Most Common Follow-ups
Q1: Why can maxFreq be stale (not updated when shrinking)?
We only care about finding windows LARGER than our current max. A stale maxFreq means we might reject windows we'd accept, but we'd never accept a window smaller than our current answer. The answer only updates when a genuinely better window is found.
3️⃣ LC #209 — Minimum Size Subarray Sum
Core Idea
Variable window. Expand right. When sum >= target, shrink left to minimize length.
Optimized → O(n) Time | O(1) Space:
java
QUBITS OF DPK
Dry Run
Input: target=7, nums=[2,3,1,2,4,3]
Output: 2 (subarray [4,3])
Key Tricks
- Expand right always, shrink left when sum >= target
- Track minimum length INSIDE the while loop (while still valid)
- Return 0 if no valid subarray found
30-Second Interview Explanation
Variable sliding window. Expand right. When sum reaches target, record length and shrink from left until invalid again. Track minimum. Time O(n), Space O(1).
️ Interview Traps
- Recording length AFTER shrinking — misses the valid state
- Not returning 0 for no valid subarray
Most Common Follow-ups
Q1: If array has negatives, does sliding window still work?
No — negative numbers break monotonicity. Use prefix sum + binary search for O(n log n) solution.