π§ DSA Mastery Roadmap
Data Structures & Algorithms for Coding Interviews β Complete C++ Reference with LeetCode Problems & Chapter Deep-Dives
13Chapters
150+Problems
C++Templates
13Patterns
π Your Progress
0%
Loadingβ¦
Ch0
Big O Notation & Recursion
0/4 solved
0%
Key Concepts
- Big O: drop constants (O(5n)=O(n)), drop lower-order terms (O(nΒ²+n)=O(nΒ²))
- Complexity Hierarchy: O(1) β O(log n) β O(n) β O(n log n) β O(nΒ²) β O(2βΏ) β O(n!)
- Recursion: every recursive function needs a base case + recursive case
- Call stack space = O(depth) β watch for stack overflow on deep recursion
- Memoization = cache results to avoid recomputing subproblems
Recursion spaceO(depth)
MemoizedO(n) states
// Generic recursion template
ReturnType solve(params, state) {
if (baseCondition) return baseValue; // 1. Base case
return solve(smallerParams, newState); // 2. Recursive case
}
// Top-down memoization
unordered_map<int, int> memo;
int dp(int n) {
if (n <= 1) return n;
if (memo.count(n)) return memo[n];
return memo[n] = dp(n-1) + dp(n-2);
}Practice Problems
| β | # | Problem | Difficulty |
|---|---|---|---|
| 1 | 509. Fibonacci Number | Easy | |
| 2 | 70. Climbing Stairs | Easy | |
| 3 | 231. Power of Two | Easy | |
| 4 | 206. Reverse Linked List | Easy |
Ch1
Arrays & Strings
0/26 solved
0%
1.1 Two Pointers
- OPPOSITE ENDS: left=0, right=n-1, move inward β palindrome, two-sum on sorted
- SAME DIRECTION: fast/slow pointers β remove duplicates, subsequence check
- TWO ARRAYS: one pointer per array β merge sorted arrays, compare sequences
TimeO(n)
SpaceO(1)
// Opposite ends
int left = 0, right = arr.size() - 1;
while (left < right) {
if (condition) { left++; right--; }
else if (tooSmall) left++;
else right--;
}
// Fast/Slow (same direction)
int slow = 0;
for (int fast = 0; fast < arr.size(); fast++)
if (condition(arr[fast])) arr[slow++] = arr[fast];| β | # | Problem | Difficulty |
|---|---|---|---|
| 1 | 125. Valid Palindrome | Easy | |
| 2 | 167. Two Sum II | Medium | |
| 3 | 344. Reverse String | Easy | |
| 4 | 977. Squares of a Sorted Array | Easy | |
| 5 | 283. Move Zeroes | Easy | |
| 6 | 26. Remove Duplicates from Sorted Array | Easy | |
| 7 | 392. Is Subsequence | Easy | |
| 8 | 15. 3Sum | Medium | |
| 9 | 11. Container With Most Water | Medium | |
| 10 | 42. Trapping Rain Water | Hard |
1.2 Sliding Window
- DYNAMIC WINDOW: expand right always, shrink left while constraint violated
- FIXED WINDOW (size k): slide β add arr[right], remove arr[right-k]
- Count of valid subarrays ending at right = right - left + 1
TimeO(n) amortized
SpaceO(1) or O(k)
// Dynamic window
int left = 0, curr = 0, ans = 0;
for (int right = 0; right < nums.size(); right++) {
curr += nums[right];
while (curr > k) curr -= nums[left++];
ans = max(ans, right - left + 1);
}
// Fixed window (size k)
int curr = 0;
for (int i = 0; i < k; i++) curr += nums[i];
int ans = curr;
for (int i = k; i < nums.size(); i++) {
curr += nums[i] - nums[i-k];
ans = max(ans, curr);
}| β | # | Problem | Difficulty |
|---|---|---|---|
| 11 | 643. Maximum Average Subarray I | Easy | |
| 12 | 1004. Max Consecutive Ones III | Medium | |
| 13 | 3. Longest Substring Without Repeating Characters | Medium | |
| 14 | 713. Subarray Product Less Than K | Medium | |
| 15 | 209. Minimum Size Subarray Sum | Medium | |
| 16 | 904. Fruit Into Baskets | Medium | |
| 17 | 239. Sliding Window Maximum | Hard | |
| 18 | 76. Minimum Window Substring | Hard |
1.3 Prefix Sum
- prefix[i] = prefix[i-1] + nums[i-1]. Range sum [l,r] = prefix[r+1] - prefix[l]
- Combine with hashmap: store prefix sum frequencies for subarray count problems
- 2D prefix sums for matrix range queries
TimeO(n) build, O(1) query
SpaceO(n)
vector<int> prefix(nums.size() + 1, 0);
for (int i = 0; i < nums.size(); i++) prefix[i+1] = prefix[i] + nums[i];
// Sum nums[l..r] = prefix[r+1] - prefix[l]
// Subarray sum equals k β O(n)
unordered_map<int,int> freq; freq[0] = 1; // init: prefix sum 0 seen once
int curr = 0, ans = 0;
for (int x : nums) { curr += x; ans += freq[curr - k]; freq[curr]++; }| β | # | Problem | Difficulty |
|---|---|---|---|
| 19 | 1480. Running Sum of 1d Array | Easy | |
| 20 | 1413. Minimum Value to Get Positive Step by Step Sum | Easy | |
| 21 | 2090. K Radius Subarray Averages | Medium | |
| 22 | 303. Range Sum Query - Immutable | Easy | |
| 23 | 560. Subarray Sum Equals K | Medium | |
| 24 | 525. Contiguous Array | Medium | |
| 25 | 238. Product of Array Except Self | Medium | |
| 26 | 724. Find Pivot Index | Easy |
Ch2
Hashing β HashMaps & Sets
0/13 solved
0%
Key Patterns
- EXISTENCE CHECK: Use unordered_set for O(1) lookup (duplicates, anagram, pangram)
- FREQUENCY COUNT: Use unordered_map<T,int> (count chars, elements, words)
- TWO-SUM PATTERN: Store seen values; for each x, check if (target-x) exists
- GROUPING: Map key β list of values (group anagrams by sorted string)
- SLIDING WINDOW + HASHMAP: Track character frequencies in a window
- PREFIX SUM + HASHMAP: Count subarrays with target sum/property
TimeO(n) average
SpaceO(n)
// Frequency count
unordered_map<int,int> freq;
for (int x : arr) freq[x]++;
// Two-sum lookup
unordered_map<int,int> seen;
for (int i = 0; i < nums.size(); i++) {
if (seen.count(target - nums[i])) return {seen[target-nums[i]], i};
seen[nums[i]] = i;
}
// Group anagrams
unordered_map<string, vector<string>> groups;
for (string& s : strs) {
string key = s; sort(key.begin(), key.end());
groups[key].push_back(s);
}| β | # | Problem | Difficulty |
|---|---|---|---|
| 1 | 1832. Check if the Sentence Is Pangram | Easy | |
| 2 | 268. Missing Number | Easy | |
| 3 | 1426. Counting Elements | Easy | |
| 4 | 1. Two Sum | Easy | |
| 5 | 383. Ransom Note | Easy | |
| 6 | 771. Jewels and Stones | Easy | |
| 7 | 2225. Find Players With Zero or One Losses | Medium | |
| 8 | 1133. Largest Unique Number | Easy | |
| 9 | 1189. Maximum Number of Balloons | Easy | |
| 10 | 49. Group Anagrams | Medium | |
| 11 | 347. Top K Frequent Elements | Medium | |
| 12 | 560. Subarray Sum Equals K | Medium | |
| 13 | 146. LRU Cache | Medium |
Ch3
Linked Lists
0/11 solved
0%
Key Patterns
- FAST/SLOW POINTERS (Floyd's): fast moves 2x, slow 1x β middle, cycle, kth from end
- REVERSAL: Iterative with prev/curr/next. O(n) time, O(1) space
- DUMMY NODE: Add dummy head to simplify edge cases (empty list, removing head)
- TWO-POINTER MERGE: Merge two sorted lists by comparing heads
TimeO(n) traversal
SpaceO(1) most patterns
// Fast/slow β find middle
ListNode *slow = head, *fast = head;
while (fast && fast->next) { slow = slow->next; fast = fast->next->next; }
// slow = middle
// Reverse iteratively
ListNode *prev = nullptr, *curr = head;
while (curr) { ListNode* nxt = curr->next; curr->next = prev; prev = curr; curr = nxt; }
// prev = new head
// Merge sorted (dummy head)
ListNode dummy(0); ListNode* tail = &dummy;
while (l1 && l2) {
if (l1->val <= l2->val) { tail->next = l1; l1 = l1->next; }
else { tail->next = l2; l2 = l2->next; }
tail = tail->next;
}
tail->next = l1 ? l1 : l2;| β | # | Problem | Difficulty |
|---|---|---|---|
| 1 | 876. Middle of the Linked List | Easy | |
| 2 | 83. Remove Duplicates from Sorted List | Easy | |
| 3 | 206. Reverse Linked List | Easy | |
| 4 | 92. Reverse Linked List II | Medium | |
| 5 | 141. Linked List Cycle | Easy | |
| 6 | 21. Merge Two Sorted Lists | Easy | |
| 7 | 19. Remove Nth Node From End of List | Medium | |
| 8 | 2. Add Two Numbers | Medium | |
| 9 | 143. Reorder List | Medium | |
| 10 | 146. LRU Cache | Medium | |
| 11 | 23. Merge K Sorted Lists | Hard |
Ch4
Stacks & Queues
0/11 solved
0%
4.1 Stacks β Key Patterns
- MATCHING/VALIDATION: Push open brackets, pop on close, check match
- MONOTONIC STACK (increasing): pop smaller than current β next greater element
- MONOTONIC STACK (decreasing): pop greater than current β next smaller element
- STRING SIMULATION: Build result char-by-char on a stack
TimeO(n)
SpaceO(n)
// Monotonic stack β Next Greater Element
vector<int> ans(nums.size(), -1);
stack<int> stk;
for (int i = 0; i < nums.size(); i++) {
while (!stk.empty() && nums[i] > nums[stk.top()])
{ ans[stk.top()] = nums[i]; stk.pop(); }
stk.push(i);
}
// Bracket matching
stack<char> stk;
for (char c : s) {
if (c == '(') stk.push(c);
else if (!stk.empty() && stk.top() == '(') stk.pop();
else return false;
}
return stk.empty();
// Deque β sliding window max
deque<int> dq;
for (int i = 0; i < nums.size(); i++) {
while (!dq.empty() && dq.front() < i-k+1) dq.pop_front();
while (!dq.empty() && nums[dq.back()] < nums[i]) dq.pop_back();
dq.push_back(i);
if (i >= k-1) ans.push_back(nums[dq.front()]);
}| β | # | Problem | Difficulty |
|---|---|---|---|
| 1 | 20. Valid Parentheses | Easy | |
| 2 | 71. Simplify Path | Medium | |
| 3 | 1544. Make The String Great | Easy | |
| 4 | 496. Next Greater Element I | Easy | |
| 5 | 901. Online Stock Span | Medium | |
| 6 | 739. Daily Temperatures | Medium | |
| 7 | 735. Asteroid Collision | Medium | |
| 8 | 84. Largest Rectangle in Histogram | Hard | |
| 9 | 346. Moving Average from Data Stream | Easy | |
| 10 | 239. Sliding Window Maximum | Hard | |
| 11 | 622. Design Circular Queue | Medium |
Ch5
Trees & Graphs
0/22 solved
0%
5.1 Binary Tree β DFS
- PREORDER (rootβleftβright): copying, serializing trees
- INORDER (leftβrootβright): sorted order for BST
- POSTORDER (leftβrightβroot): deletion, computing subtree properties
- GLOBAL vs LOCAL: use a global variable for answers spanning multiple nodes
TimeO(n)
SpaceO(h) stack
// Recursive DFS
int dfs(TreeNode* node) {
if (!node) return 0;
int left = dfs(node->left), right = dfs(node->right);
return 1 + max(left, right); // example: height
}
// BFS level-order
queue<TreeNode*> q; q.push(root);
while (!q.empty()) {
int sz = q.size();
for (int i = 0; i < sz; i++) {
TreeNode* node = q.front(); q.pop();
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
// Graph BFS β shortest path
vector<int> dist(n, -1);
queue<int> q; q.push(start); dist[start] = 0;
while (!q.empty()) {
int node = q.front(); q.pop();
for (int nei : adj[node])
if (dist[nei]==-1) { dist[nei]=dist[node]+1; q.push(nei); }
}| β | # | Problem | Difficulty |
|---|---|---|---|
| 1 | 111. Minimum Depth of Binary Tree | Easy | |
| 2 | 104. Maximum Depth of Binary Tree | Easy | |
| 3 | 543. Diameter of Binary Tree | Easy | |
| 4 | 1026. Maximum Difference Between Node and Ancestor | Medium | |
| 5 | 113. Path Sum II | Medium | |
| 6 | 236. Lowest Common Ancestor of a Binary Tree | Medium | |
| 7 | 124. Binary Tree Maximum Path Sum | Hard | |
| 8 | 102. Binary Tree Level Order Traversal | Medium | |
| 9 | 1302. Deepest Leaves Sum | Medium | |
| 10 | 103. Binary Tree Zigzag Level Order Traversal | Medium | |
| 11 | 637. Average of Levels in Binary Tree | Easy | |
| 12 | 701. Insert into a Binary Search Tree | Medium | |
| 13 | 270. Closest Binary Search Tree Value | Easy | |
| 14 | 98. Validate Binary Search Tree | Medium | |
| 15 | 230. Kth Smallest Element in a BST | Medium | |
| 16 | 1971. Find if Path Exists in Graph | Easy | |
| 17 | 695. Max Area of Island | Medium | |
| 18 | 1926. Nearest Exit from Entrance in Maze | Medium | |
| 19 | 200. Number of Islands | Medium | |
| 20 | 207. Course Schedule | Medium | |
| 21 | 133. Clone Graph | Medium | |
| 22 | 127. Word Ladder | Hard |
Ch6
Heaps & Priority Queues
0/8 solved
0%
Key Patterns
- TOP K: Min-heap of size k β push each element; if size > k, pop. Heap = top k largest
- K-WAY MERGE: Push (value, listIndex, elemIndex) into heap; always pop smallest
- RUNNING MEDIAN: Max-heap for lower half + min-heap for upper half
- Trigger: 'Smallest/Largest K elements' β heap. C++ default is max-heap; use greater<int> for min
Push/PopO(log n)
PeekO(1)
// Min-heap
priority_queue<int, vector<int>, greater<int>> minH;
// Top-K largest using min-heap of size K
priority_queue<int, vector<int>, greater<int>> pq;
for (int x : nums) { pq.push(x); if (pq.size() > k) pq.pop(); }
// pq.top() = kth largest
// Custom comparator
auto cmp = [](pair<int,int>& a, pair<int,int>& b){ return a.first > b.first; };
priority_queue<pair<int,int>, vector<pair<int,int>>, decltype(cmp)> pq(cmp);| β | # | Problem | Difficulty |
|---|---|---|---|
| 1 | 1962. Remove Stones to Minimize the Total | Medium | |
| 2 | 1167. Minimum Cost to Connect Sticks | Medium | |
| 3 | 215. Kth Largest Element in an Array | Medium | |
| 4 | 973. K Closest Points to Origin | Medium | |
| 5 | 703. Kth Largest Element in a Stream | Easy | |
| 6 | 295. Find Median from Data Stream | Hard | |
| 7 | 621. Task Scheduler | Medium | |
| 8 | 23. Merge K Sorted Lists | Hard |
Ch7
Greedy Algorithms
0/8 solved
0%
Key Patterns
- SORT FIRST: Most greedy problems require sorting by some key (weight, ratio, end time)
- INTERVAL SCHEDULING: Sort by end time β greedily take non-overlapping intervals
- EXCHANGE ARGUMENT: Prove swapping adjacent elements doesn't improve solution
- Ask: 'Does taking the best local choice block a better global solution?' If no β greedy works
TimeO(n log n) with sort
SpaceO(1)
// Interval scheduling β max non-overlapping intervals
sort(intervals.begin(), intervals.end(),
[](auto& a, auto& b){ return a[1] < b[1]; }); // sort by end
int count = 0, prevEnd = INT_MIN;
for (auto& iv : intervals)
if (iv[0] >= prevEnd) { count++; prevEnd = iv[1]; }| β | # | Problem | Difficulty |
|---|---|---|---|
| 1 | 1323. Maximum 69 Number | Easy | |
| 2 | 1710. Maximum Units on a Truck | Easy | |
| 3 | 1338. Reduce Array Size to The Half | Medium | |
| 4 | 435. Non-overlapping Intervals | Medium | |
| 5 | 55. Jump Game | Medium | |
| 6 | 45. Jump Game II | Medium | |
| 7 | 134. Gas Station | Medium | |
| 8 | 135. Candy | Hard |
Ch8
Binary Search
0/9 solved
0%
Key Patterns
- ON SORTED ARRAY: Classic search / find leftmost or rightmost position
- ON ANSWER SPACE: Binary search on the answer when feasibility is monotonic
- FIND LEFTMOST: Use 'left = mid + 1' when condition met β pushes left boundary right
- Trigger: 'minimize the maximum' or 'maximize the minimum' β binary search on answer
TimeO(log n)
SpaceO(1)
// Standard binary search
int lo = 0, hi = nums.size()-1;
while (lo <= hi) {
int mid = lo + (hi-lo)/2; // avoid overflow
if (nums[mid] == target) return mid;
else if (nums[mid] < target) lo = mid+1;
else hi = mid-1;
}
// Binary search on answer (minimise)
int lo = minVal, hi = maxVal, ans = hi;
while (lo <= hi) {
int mid = lo + (hi-lo)/2;
if (feasible(mid)) { ans = mid; hi = mid-1; }
else lo = mid+1;
}| β | # | Problem | Difficulty |
|---|---|---|---|
| 1 | 704. Binary Search | Easy | |
| 2 | 35. Search Insert Position | Easy | |
| 3 | 2389. Longest Subsequence With Limited Sum | Easy | |
| 4 | 1283. Find the Smallest Divisor Given a Threshold | Medium | |
| 5 | 410. Split Array Largest Sum | Hard | |
| 6 | 875. Koko Eating Bananas | Medium | |
| 7 | 162. Find Peak Element | Medium | |
| 8 | 33. Search in Rotated Sorted Array | Medium | |
| 9 | 4. Median of Two Sorted Arrays | Hard |
Ch9
Backtracking
0/11 solved
0%
Key Patterns
- GENERATION: Build all permutations/subsets/combinations β just enumerate
- CONSTRAINED: Prune early when constraint violated (sum exceeds target)
- Template: choose β recurse β unchoose (restore state)
- Use 'start' index to avoid re-using earlier elements in combination problems
- Use 'used[]' boolean array for permutations to avoid duplicate positions
- Time: O(n! Γ n) permutations, O(2βΏ Γ n) subsets β always exponential
SubsetsO(2βΏ Γ n)
PermsO(n! Γ n)
vector<vector<int>> result;
vector<int> current;
void backtrack(int start) {
if (isComplete()) { result.push_back(current); return; }
for (int i = start; i < candidates.size(); i++) {
if (shouldPrune(i)) continue;
current.push_back(candidates[i]); // choose
backtrack(i + 1); // recurse
current.pop_back(); // unchoose
}
}| β | # | Problem | Difficulty |
|---|---|---|---|
| 1 | 797. All Paths From Source to Target | Medium | |
| 2 | 17. Letter Combinations of a Phone Number | Medium | |
| 3 | 22. Generate Parentheses | Medium | |
| 4 | 967. Numbers With Same Consecutive Differences | Medium | |
| 5 | 216. Combination Sum III | Medium | |
| 6 | 78. Subsets | Medium | |
| 7 | 46. Permutations | Medium | |
| 8 | 39. Combination Sum | Medium | |
| 9 | 79. Word Search | Medium | |
| 10 | 51. N-Queens | Hard | |
| 11 | 37. Sudoku Solver | Hard |
Ch10
Dynamic Programming
0/11 solved
0%
Key Patterns
- Step 1: Define what dp[i] (or dp[i][j]) represents
- Step 2: Find the recurrence relation (transition)
- Step 3: Identify base cases
- Step 4: Determine iteration order (ensure subproblems solved before needed)
- DP vs Greedy: DP explores all choices; Greedy makes one. Use DP when Greedy fails
1D DPO(n)
2D DPO(mΓn)
// 1D DP β Climbing Stairs
vector<int> dp(n+1, 0); dp[0]=1; dp[1]=1;
for (int i = 2; i <= n; i++) dp[i] = dp[i-1] + dp[i-2];
// Coin Change (unbounded knapsack)
vector<int> dp(amount+1, INT_MAX); dp[0]=0;
for (int i = 1; i <= amount; i++)
for (int coin : coins)
if (coin<=i && dp[i-coin]!=INT_MAX) dp[i]=min(dp[i],dp[i-coin]+1);
// State machine DP (Stock with cooldown)
int hold = -prices[0], sold = 0, rest = 0;
for (int i = 1; i < prices.size(); i++) {
int ph=hold,ps=sold,pr=rest;
hold=max(ph,pr-prices[i]); sold=ph+prices[i]; rest=max(pr,ps);
}| β | # | Problem | Difficulty |
|---|---|---|---|
| 1 | 70. Climbing Stairs | Easy | |
| 2 | 746. Min Cost Climbing Stairs | Easy | |
| 3 | 322. Coin Change | Medium | |
| 4 | 714. Best Time to Buy and Sell Stock with Transaction Fee | Medium | |
| 5 | 309. Best Time to Buy and Sell Stock with Cooldown | Medium | |
| 6 | 63. Unique Paths II | Medium | |
| 7 | 931. Minimum Falling Path Sum | Medium | |
| 8 | 198. House Robber | Medium | |
| 9 | 1143. Longest Common Subsequence | Medium | |
| 10 | 72. Edit Distance | Medium | |
| 11 | 139. Word Break | Medium |
Ch11
Bonus Topics β Tries, Bit Manipulation, Intervals
0/6 solved
0%
11.1 Tries (Prefix Trees)
- Store strings character by character. Each node = one character; isEnd flag marks valid word
- O(m) insert and search where m = string length. Use for: autocomplete, prefix search
struct TrieNode {
unordered_map<char, TrieNode*> children;
bool isEnd = false;
};
class Trie {
TrieNode* root = new TrieNode();
public:
void insert(string word) {
TrieNode* curr = root;
for (char c : word) {
if (!curr->children.count(c)) curr->children[c] = new TrieNode();
curr = curr->children[c];
}
curr->isEnd = true;
}
bool search(string word) {
TrieNode* curr = root;
for (char c : word) {
if (!curr->children.count(c)) return false;
curr = curr->children[c];
}
return curr->isEnd;
}
};11.2 Bit Manipulation
- POWER OF TWO:
n > 0 && (n & (n-1)) == 0β exactly one bit set - CLEAR LOWEST SET BIT:
n & (n-1)β use in Kernighan popcount loop O(set bits) - ISOLATE LOWEST SET BIT:
n & (-n)β rightmost 1 preserved, rest zero - XOR TRICK:
a^a=0, a^0=aβ cancel pairs; find unique element in O(n)/O(1) - SET / CLEAR / TOGGLE / CHECK:
x|=(1<<n)/x&=~(1<<n)/x^=(1<<n)/(x>>n)&1 - EXTRACT BITFIELD:
(x >> start) & ((1 << width) - 1)β packet headers, hardware registers - BITMASK DP: state = subset bitmask of N elements; viable when N β€ 20
Bit opsO(1)
Popcount loopO(set bits)
Bitmask DPO(2βΏ Β· n)
// Core mask operations
x |= (1 << n); // SET bit n
x &= ~(1 << n); // CLEAR bit n
x ^= (1 << n); // TOGGLE bit n
int b = (x >> n) & 1; // CHECK bit n (returns 0 or 1)
// Power of two?
bool isPow2 = x > 0 && (x & (x-1)) == 0;
// Count set bits β Brian Kernighan O(set bits)
int cnt = 0;
while (x) { x &= x-1; cnt++; } // clears lowest set bit each iteration
// XOR β find single non-duplicate in O(n) / O(1) (LC 136)
int res = 0;
for (int n : nums) res ^= n; // paired elements cancel: x^x=0
// Count bits for 0..n β DP O(n) (LC 338)
vector<int> dp(n+1);
for (int i = 1; i <= n; i++) dp[i] = dp[i >> 1] + (i & 1);
// Extract bitfield [start, start+width)
uint32_t field = (x >> start) & ((1 << width) - 1);
// Insert pattern: x = (x & ~mask) | (pattern << start)
// where mask = ((1 << width) - 1) << start
βοΈ Full Deep-Dive (10 tabs)
π§ Systems Problems
π Debugging Guide
π― Practice Problems β
Practice Problems
| β | # | Problem | Difficulty |
|---|---|---|---|
| 1 | 208. Implement Trie (Prefix Tree) | Medium | |
| 2 | 1268. Search Suggestions System | Medium | |
| 3 | 136. Single Number (XOR) | Easy | |
| 4 | 191. Number of 1 Bits | Easy | |
| 5 | 57. Insert Interval | Medium | |
| 6 | 56. Merge Intervals | Medium |
Ch12
Study Plan & Complexity Cheatsheet
Phase 1 β Foundations (Weeks 1β4)
- Week 1: Ch0 Big O + Ch1 Two Pointers. Solve all Easy problems.
- Week 2: Ch1 Sliding Window + Prefix Sum. Solve all Easy + 2 Medium.
- Week 3: Ch2 Hashing. Core patterns β Two Sum, freq count, grouping.
- Week 4: Ch3 Linked Lists. All patterns β fast/slow, reversal, merge.
Phase 2 β Core (Weeks 5β9)
- Week 5: Ch4 Stacks (monotonic, bracket matching).
- Week 6β7: Ch5 Trees β DFS first (preorder, postorder, LCA), then BFS + BST.
- Week 8: Ch5 Graphs β DFS + BFS + Union-Find.
- Week 9: Ch6 Heaps + Ch7 Greedy.
Phase 3 β Advanced (Weeks 10β13)
- Week 10: Ch8 Binary Search β standard + answer space search.
- Week 11: Ch9 Backtracking β all generation/constrained problems.
- Week 12β13: Ch10 DP β 1D, 2D, state machine, knapsack.
Complexity Cheatsheet
Array / String
| Access | O(1) |
| Search | O(n) |
| Insert/Delete | O(n) |
| Append | O(1) amort. |
HashMap / HashSet
| Insert | O(1) avg |
| Lookup | O(1) avg |
| Delete | O(1) avg |
| Worst case | O(n) |
Binary Tree
| DFS / BFS | O(n) |
| BST search | O(h) |
| BST balanced | O(log n) |
| Space | O(h) stack |
Heap
| Push | O(log n) |
| Pop | O(log n) |
| Peek | O(1) |
| Build | O(n) |
Sorting
| std::sort | O(n log n) |
| Counting sort | O(n+k) |
| Radix sort | O(nk) |
| Merge sort | O(n log n) |
Graph
| BFS / DFS | O(V+E) |
| Dijkstra | O((V+E)log V) |
| Union-Find | O(Ξ±(n))βO(1) |
| Topo sort | O(V+E) |