Cosmic Module
D
Qubits of DPK
March 15, 2026
Core DSA
1️⃣ LC #76 — Minimum Window Substring
Core Idea
Sliding window with frequency maps. Expand right until all chars covered. Shrink left to minimize. Track valid state with formed counter.
Optimized → O(n+m) Time | O(n+m) Space:
java
QUBITS OF DPK
Dry Run
Input: s = "ADOBECODEBANC", t = "ABC"
need: {A:1, B:1, C:1}, required=3
Output: "BANC"
Key Tricks
- formed counter tracks how many required chars have met their frequency — avoid scanning map each step
- Use equals() for Integer comparison, not ==
- Record answer inside shrink loop when window is still valid
30-Second Interview Explanation
Two HashMaps: need (from t) and window (current). Expand right until all t chars covered (formed == required). Shrink left to minimize while still valid. Record minimum window. Time O(n+m), Space O(n+m).
️ Interview Traps
- Using == for Integer comparison — fails for values > 127 (Integer cache boundary)
- Not tracking formed — would need O(|t|) check each step
Most Common Follow-ups
Q1: Why use formed counter instead of checking map equality each step?
Checking map equality is O(|t|) per step — makes total O(n*|t|). The formed counter tracks satisfied characters in O(1) per step. Increment when a char's window count first meets its need, decrement when it drops below. Checking formed == required is O(1).
2️⃣ LC #239 — Sliding Window Maximum
Core Idea
Monotonic deque stores indices of decreasing elements. Front always = max in current window.
Optimized → O(n) Time | O(k) Space:
java
QUBITS OF DPK
Dry Run
Input: nums = [1,3,-1,-3,5,3,6,7], k=3
Output: [3,3,5,5,6,7]
Key Tricks
- Deque stores INDICES not values — needed for out-of-window check
- Remove from front when out of window
- Remove from back when new element is larger — they can never be max
- Front of deque = max of current window
30-Second Interview Explanation
Monotonic decreasing deque of indices. For each element: remove expired indices from front. Remove smaller elements from back (they'll never be max). Add current. Front = window max. Time O(n), Space O(k).
️ Interview Traps
- Storing values instead of indices — can't check if element is out of window
- Not removing out-of-window elements from front first
Most Common Follow-ups
Q1: Sliding Window Minimum?
Identical approach but maintain INCREASING deque. Remove from back when new element is SMALLER. Front = window minimum.
Q2: Why can we discard smaller elements when a larger element arrives?
If nums[i] > nums[j] where i > j, then j will leave the window before i. So whenever j is in the window, i is also in the window and is larger. j can NEVER be the maximum. Safe to discard permanently.