A personal collection of classic algorithms and data structure problems β with clear implementations and quick-reference docs for interview prep.
Each problem lives in its own folder with:
- β A Python implementation
- π A
README.mdwith the core idea, visual diagram, history, interview tips, and use cases
| Problem | Time | Space | Key Pattern |
|---|---|---|---|
| BFS | O(V+E) | O(V) | Queue, level-by-level |
| DFS | O(V+E) | O(V) | Recursion / stack |
| Binary Search | O(log n) | O(1) | Divide sorted range |
| Grid Traversal | O(mΒ·n) | O(mΒ·n) | BFS/DFS on 2D matrix |
| Shortest Path | O(V+E) | O(V) | BFS (unweighted) |
| Problem | Time | Space | Key Pattern |
|---|---|---|---|
| Bucket Sort / Top-K Frequent | O(n) | O(n) | Frequency buckets |
| Cyclic Sort | O(n) | O(1) | Place num at index num-1 |
| Dutch National Flag | O(n) | O(1) | 3-pointer partition |
| Sort Colors | O(n) | O(1) | Counting sort |
| Merge Sorted Arrays | O(n+m) | O(n+m) | Two pointers |
| Merge K Sorted Lists | O(n log k) | O(k) | Divide & conquer / heap |
| Topological Sort | O(V+E) | O(V) | DFS post-order + reverse |
| Problem | Time | Space | Key Pattern |
|---|---|---|---|
| Reverse Linked List | O(n) | O(1) | 3-pointer iteration |
| Detect Cycle (Linked List) | O(n) | O(1) | Floyd's tortoise & hare |
| Rotate Linked List | O(n) | O(1) | Make circular, find new tail |
| Problem | Time | Space | Key Pattern |
|---|---|---|---|
| Detect Cycle (Graph) | O(V+E) | O(V) | DFS with visited set |
| N Provinces | O(nΒ²) | O(n) | Connected components DFS |
| Recursive Deps | O(V+E) | O(V) | Post-order DFS |
| Longest Leaf (Tree Height) | O(n) | O(h) | Post-order DFS |
| N-th Level Leafs | O(n) | O(h) | DFS with depth |
| House Robber III | O(n) | O(h) | Tree DP (skip/take pair) |
| Problem | Time | Space | Key Pattern |
|---|---|---|---|
| Knapsack 0/1 | O(nΒ·W) | O(nΒ·W) | Include/exclude DP |
| Longest Consecutive Sequence | O(n) | O(n) | Set + sequence root |
| Longest Increasing Subsequence | O(nΒ²) | O(n) | DP / patience sort |
| Longest Common Subsequence | O(mΒ·n) | O(mΒ·n) | 2D DP |
| Max Subarray (Kadane's) | O(n) | O(1) | Extend or restart |
| Problem | Time | Space | Key Pattern |
|---|---|---|---|
| Longest Substring (No Repeat) | O(n) | O(m) | Set + shrink left |
| Character Replacement | O(n) | O(1) | max_freq window trick |
| Problem | Time | Space | Key Pattern |
|---|---|---|---|
| Is Anagram | O(n) | O(1) | Counter comparison |
| Is Palindrome | O(n) | O(1) | Two pointers |
| Problem | Time | Space | Key Pattern |
|---|---|---|---|
| Combination Sum | O(n^(t/m)) | O(t/m) | DFS include/skip (reuse OK) |
| Combination Sum II | O(2^n) | O(n) | Sort + skip duplicates |
| Permutations | O(n!Β·n) | O(n) | All orderings backtrack |
| Subsets | O(nΒ·2^n) | O(n) | Include/exclude each element |
| Problem | Time | Space | Key Pattern |
|---|---|---|---|
| Heap / Priority Queue | O(log n) push/pop | O(n) | Min-heap, heapq |
| Problem | Time | Space | Key Pattern |
|---|---|---|---|
| Rotate 90Β° | O(nΒ²) | O(1) | Reverse rows + transpose |
- Browse by category using the table above
- Open any folder β the
README.mdhas the theory, the.pyfile has the code - For interview prep: read the README first for the key insight, then try to implement from memory
π problem-name/
βββ problem.py β implementation
βββ README.md β explanation + interview tips
| Pattern | Problems |
|---|---|
| Two Pointers | Palindrome, Merge Sorted, Dutch National Flag |
| Sliding Window | Longest Substring, Character Replacement |
| BFS/DFS | Graph traversal, Grid, Shortest Path, Connected Components |
| Backtracking | Subsets, Permutations, Combination Sum |
| Dynamic Programming | Knapsack, LCS, LIS, Max Subarray, House Robber |
| Divide & Conquer | Merge K Lists, Binary Search |
| Topological Sort | Topo Sort, Recursive Deps, Detect Cycle |
Happy grinding! π―