Cosmic Module
D
Qubits of DPK
March 12, 2026
Core DSA
1️⃣ LC #1 — Two Sum
Core Idea
For each element, check if complement (target - current) exists in HashMap. Check BEFORE putting — prevents self-match.
Brute Force → O(n²) Time | O(1) Space:
java
QUBITS OF DPK
Optimized → O(n) Time | O(n) Space:
java
QUBITS OF DPK
Dry Run
Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]
Trap case: nums = [3, 3], target = 6
Output: [0, 1] (check-before-put prevents self-match)
Key Tricks
- Check BEFORE put — prevents using same element twice
- Map stores value → index (not index → value)
- One pass is sufficient
30-Second Interview Explanation
For each element compute complement = target - current. If complement in map, found our pair. Check before put prevents self-matching. Time O(n), Space O(n).
️ Interview Traps
- Putting before checking → self-match bug for target = 2 * nums[i]
- Returning values instead of indices
Most Common Follow-ups
Q1: Sorted array — O(1) space?
java
QUBITS OF DPK
Time O(n), Space O(1).
Q2: Return all pairs.
Don't return on first match — collect in ArrayList. Continue iterating. Handle duplicates with careful overwriting.
Q3: Three Sum (LC #15)?
Sort array. Fix one element, use two pointers on remaining subarray to find pairs summing to -nums[i]. Skip duplicates at each level. Time O(n²), Space O(1) extra.
2️⃣ LC #217 — Contains Duplicate
Core Idea
HashSet — check contains before add. Return true on first duplicate.
Brute Force → O(n²) | Optimized → O(n) Time | O(n) Space:
java
QUBITS OF DPK
Dry Run
Input: nums = [1, 2, 3, 1]
Output: true
Key Tricks
- HashSet not HashMap — only need existence
- Check before add — catches duplicate on second occurrence
- Early return on first duplicate
30-Second Interview Explanation
HashSet tracking seen elements. Check contains before add. Return true on first duplicate. Time O(n), Space O(n).
️ Interview Traps
- Using HashMap unnecessarily
Most Common Follow-ups
Q1: LC #219 — Duplicate within k distance.
java
QUBITS OF DPK
Sliding window HashSet of size k. Time O(n), Space O(k).
Q2: LC #220 — Value difference ≤ t within distance ≤ k.
TreeSet sliding window. Query floor(num+t) — check if >= num-t. Time O(n log k), Space O(k).
3️⃣ LC #242 — Valid Anagram
Core Idea
Frequency count. Increment for s, decrement for t. All zeros at end = anagram.
Brute Force → O(n log n): Sort both, compare.
Optimized → O(n) Time | O(1) Space:
java
QUBITS OF DPK
Dry Run
Input: s = "anagram", t = "nagaram"
Processing simultaneously (increment for s[i], decrement for t[i]):
All freq values = 0 → Output: true
Key Tricks
- charAt(i) - 'a' maps char to index 0–25
- Increment for s, decrement for t — net zero = anagram
- Length check early exit
30-Second Interview Explanation
Frequency array of 26. Increment for s, decrement for t. All zeros → anagram. Time O(n), Space O(1).
️ Interview Traps
- Not handling different lengths upfront
Most Common Follow-ups
Q1: Unicode characters?
java
QUBITS OF DPK
Q2: LC #49 — Group Anagrams.
java
QUBITS OF DPK
Time O(nL), Space O(nL).
4️⃣ LC #387 — First Unique Character in a String
Core Idea
Two passes: build frequency, then traverse ORIGINAL STRING for first count == 1.
Optimized → O(n) Time | O(1) Space:
java
QUBITS OF DPK
Dry Run
Input: s = "leetcode"
Pass 1 — build frequency:
Pass 2 — find first with count 1:
Output: 0
Key Tricks
- Second pass on ORIGINAL STRING — not the map (no order in map)
- Single pass impossible — need complete frequency first
30-Second Interview Explanation
First pass builds frequency. Second pass iterates original string returning first index with frequency 1. Can't do single pass — need complete count to confirm uniqueness. Time O(n), Space O(1).
️ Interview Traps
- Iterating HashMap in second pass — no guaranteed order
Most Common Follow-ups
Q1: String is a stream?
Use LinkedHashMap to maintain insertion order + frequency. After stream ends, iterate LinkedHashMap for first entry with count 1. Preserves order while tracking frequency.
Q2: Return character, not index?
Return s.charAt(i) instead of i. Return ' ' or '\0' as sentinel for no unique character.
5️⃣ LC #349 — Intersection of Two Arrays
Core Idea
HashSet from nums1. Check each num in nums2. ResultSet deduplicates.
Optimized → O(n+m) Time | O(n+m) Space:
java
QUBITS OF DPK
Dry Run
Input: nums1 = [4,9,9,5], nums2 = [9,4,9,8,4]
Build set1: {4, 9, 5}
Output: [9, 4] (order may vary)
Key Tricks
- Build from LARGER array, iterate SMALLER (performance)
- ResultSet automatically deduplicates
- No sorting needed
30-Second Interview Explanation
HashSet from nums1. Iterate nums2 — if exists in set, add to resultSet. ResultSet deduplicates. Convert to array. Time O(n+m), Space O(n+m).
️ Interview Traps
- Using ArrayList for result → needs manual duplicate check
Most Common Follow-ups
Q1: Both arrays sorted — O(1) space?
java
QUBITS OF DPK
Q2: One array very large, other small?
Build HashSet from SMALLER array only. Iterate larger. Minimizes memory to O(min(n,m)) space.
6️⃣ LC #350 — Intersection of Two Arrays II
Core Idea
HashMap for frequency. Decrement on match. REMOVE key at zero to prevent false matches.
Optimized → O(n+m) Time | O(min(n,m)) Space:
java
QUBITS OF DPK
Dry Run
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Build freqMap from nums2: {2:2}
Output: [2, 2]
Key Tricks
- REMOVE key at count 0 — prevents false match on third occurrence
- Build from smaller array for space efficiency
- ArrayList because result size unknown
30-Second Interview Explanation
HashMap frequency from smaller array. Iterate larger — on match, add to result, decrement count, remove at zero. Time O(n+m), Space O(min(n,m)).
️ Interview Traps
- Not removing key at zero → third occurrence incorrectly matches
Most Common Follow-ups
Q1: Arrays are sorted — two pointer approach.
java
QUBITS OF DPK
Time O(n+m), Space O(1) extra.
Q2: nums2 stored on disk (too large)?
Load nums1 into HashMap. Stream nums2 in chunks from disk. Process each chunk against HashMap. Only nums1 (smaller) stays in memory.
7️⃣ LC #448 — Find All Numbers Disappeared in an Array
Core Idea
Index marking: negate nums[abs(nums[i])-1] to mark presence. Positive in second pass = missing.
O(1) Space → O(n) Time | O(1) Space:
java
QUBITS OF DPK
Dry Run
Input: nums = [4, 3, 2, 7, 8, 2, 3, 1], n=8
Pass 1 — mark negatives:
Pass 2 — find positives (missing numbers):
Output: [5, 6]
Key Tricks
- Math.abs(nums[i]) — value may already be negative from previous marking
- Mark negative only if currently positive — prevents double-negation
- Positive in second pass = i+1 never appeared as a value
30-Second Interview Explanation
Use values as indices to mark presence by negating. Second pass finds positive positions = missing numbers. Time O(n), Space O(1).
️ Interview Traps
- Forgetting Math.abs when computing index → already-negative values give wrong index → ArrayIndexOutOfBounds
Most Common Follow-ups
Q1: Without modifying input — HashSet approach.
java
QUBITS OF DPK
Time O(n), Space O(n).
Q2: LC #41 — First Missing Positive (similar concept, harder).
Place each number at its correct index position (nums[i] should be at index nums[i]-1). Scan for first index where nums[i] != i+1. Time O(n), Space O(1).
8️⃣ LC #383 — Ransom Note
Core Idea
Build frequency from magazine (source). Decrement for ransomNote (demand). Negative = can't construct.
Optimized → O(n+m) Time | O(1) Space:
java
QUBITS OF DPK
Dry Run
Input: ransomNote = "aa", magazine = "aab"
Build freq from magazine:
Process ransomNote:
Output: true
Input: ransomNote = "aab", magazine = "aa" → freq[a]=2, freq[b]=0. On 'b': 0-1=-1 < 0 → false
Key Tricks
- Build from magazine (supply), decrement for ransomNote (demand)
- Early exit when count goes negative
- int[26] for O(1) space
30-Second Interview Explanation
Frequency array from magazine. For each char in ransomNote, decrement. Goes negative → can't supply → false. Time O(n+m), Space O(1).
️ Interview Traps
- Confusing which string to build from and which to decrement
Most Common Follow-ups
Q1: Unicode characters?
java
QUBITS OF DPK
Q2: Each magazine letter used at most once?
Already the case — each decrement consumes one occurrence. Problem models this exactly.
9️⃣ LC #202 — Happy Number
Core Idea
Sum of squared digits. Non-happy numbers always cycle. HashSet detects cycle.
HashSet → O(log n) per step:
java
QUBITS OF DPK
Dry Run
Input: n = 19
num == 1 → Output: true
Bug demo — without sum = 0 reset:
Wrong from iteration 2 onward!
Key Tricks
- seen.add(num) BEFORE computing next — so revisit is caught
- sum = 0 reset at end of each iteration — critical bug if forgotten
- Non-happy numbers always cycle through: 4→16→37→58→89→145→42→20→4
30-Second Interview Explanation
Compute sum of squared digits repeatedly. Non-happy numbers always cycle — HashSet detects revisit. Reset sum each iteration. Time O(log n) per step, Space O(log n).
️ Interview Traps
- Forgetting sum = 0 reset → previous sums bleed into next iteration
Most Common Follow-ups
Q1: O(1) space — Floyd's cycle detection.
java
QUBITS OF DPK
Slow moves 1 step, fast moves 2. If cycle exists, fast laps slow and they meet. If no cycle (reaches 1), fast gets there first.
Q2: What cycle do non-happy numbers enter?
Fixed cycle: 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4. You can hardcode: check n == 4 inside loop as O(1) space shortcut.
LC #205 — Isomorphic Strings
Core Idea
Two HashMaps — one for s→t, one for t→s. Both must be consistent (bijection).
Optimized → O(n) Time | O(1) Space:
java
QUBITS OF DPK
Dry Run
Input: s = "ab", t = "aa" (one map failure case)
With only mapST:
Returns true — WRONG!
With mapTS catching it:
Output: false
Input: s = "egg", t = "add" (valid isomorphic)
Output: true
Key Tricks
- ONE map is NOT enough — misses many-to-one mapping
- Both directions must be checked for bijection
- Space is O(1) — at most 256 unique characters
30-Second Interview Explanation
Two maps enforcing bidirectional consistency. One-way mapping allows many-to-one which is invalid. Both maps must agree at every step. Time O(n), Space O(1).
️ Interview Traps
- One map alone → misses different s chars mapping to same t char
Most Common Follow-ups
Q1: Why does one map fail? Concrete example.
s="ab", t="aa". With only mapST: 'a'→'a' and 'b'→'a' both added without conflict. But two different s-chars map to same t-char — not isomorphic. mapTS catches this: when 'a' in t tries to map to both 'a' and 'b' in s, conflict detected on second occurrence.
Q2: LC #290 — Word Pattern (same concept at word level).
java
QUBITS OF DPK