One question a day to ensure a sharp mind.
L0: straight forward questionL1: variance of templateL2: need to think for a while / complex implementationL3: need aha moment / unexpected algorithm
Study-order progression through all topic areas. Each entry links to the topic README with templates and notes.
| # | Topic | README | Description |
|---|---|---|---|
| 1 | String | README | String operations, Counter API |
| 2 | Hash Table | README | Counter operations, mapping |
| 3 | Linked List | README | Reverse, fast/slow pointers |
| 4 | Two Pointers | README | Same/different direction templates |
| 5 | Sliding Window | README | Fixed/dynamic window, at-most trick |
| 6 | Prefix Sum | README | 1D/2D prefix sum, difference array |
| 7 | Binary Search | README | Template, bisect module |
| 8 | Stack | README | Monotonic stack, Eulerian path, tree traversal |
| 9 | Monotonic Queue | README | Sliding window max/min by deque |
| 10 | Histogram | README | Histogram model for matrices |
| # | Topic | README | Description |
|---|---|---|---|
| 11 | BFS | README | Graph BFS template |
| 12 | BFS Tree | README | Tree level-order traversal |
| 13 | BFS/DFS Graph | README | Matrix and graph DFS |
| 14 | Dijkstra | README | Single-source shortest path |
| 15 | Floyd-Warshall | README | All-pairs shortest path |
| 16 | DFS Tree | README | Tree DFS traversal templates |
| 17 | LCA | README | Lowest common ancestor, binary lifting |
| 18 | Backtracking | README | Pruning, array/graph templates |
| # | Topic | README | Description |
|---|---|---|---|
| 19 | DP Overview | README | Categories, matrix exponentiation |
| 20 | Knapsack | README | 0/1 and unbounded knapsack |
| 21 | LIS | README | O(n^2) and O(n log n) |
| 22 | Longest Subsequence | README | LIS/LCS templates |
| 23 | Kadane | README | Maximum subarray |
| 24 | Digit DP | README | Digit DP templates with bounds |
| # | Topic | README | Description |
|---|---|---|---|
| 25 | Greedy | README | Assign/interval problems |
| 26 | Math | README | GCD/LCM, sieve, math functions |
| 27 | Combinatorics | README | Product rule, C(n,k), permutations |
| 28 | Primes | README | Sieve, factorization, LPF |
| 29 | Bezout's Lemma | README | Extended GCD |
| 30 | Game Theory | README | Minimax |
| 31 | Probability | README | Reservoir sampling, shuffle |
| 32 | Bit Manipulation | README | Bit operations, bitmask DP |
| # | Topic | README | Description |
|---|---|---|---|
| 33 | Trie | README | Insert, search, prefix |
| 34 | Union-Find | README | Path compression, union by rank, Kruskal's |
| 35 | Binary Indexed Tree | README | Fenwick tree |
| 36 | Segment Tree | README | Tree/array/ZKW, lazy propagation |
| 37 | Sorted Containers | README | SortedList complexity reference |
| # | Topic | README | Description |
|---|---|---|---|
| 38 | Sorting | README | Cycle sort |
| 39 | KMP | README | KMP pattern matching |
| 40 | Z-Function | README | Z-function pattern matching |
| 41 | Prim | README | Minimum spanning tree |
| 42 | Majority Voting | README | Boyer-Moore voting |
| 43 | Rolling Hash | README | Rabin-Karp |
| 44 | Greedy Heap | README | Heap-based greedy patterns |
| 45 | SQL | README | Query categories |
| 46 | System Design | README | General steps |
Solutions depend on header.py for shared imports and class definitions. Use the runner script:
python run.py path/to/solution.pyThis pre-loads header.py into the namespace before executing the solution file.
Row: input size(IS), column: time complexity(TC)
| Input Size | O( |
O( |
O( |
O( |
O(nlogn) | O(n) | O(logn) | O(1) |
|---|---|---|---|---|---|---|---|---|
| 1-10 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| 10-50 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| 50-100 | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| 100-500 | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| 500 - |
✗ | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
✗ | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
✗ | ✗ | ✗ | ? | ✓ | ✓ | ✓ | ✓ |
|
|
✗ | ✗ | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ |
|
|
✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✓ | ✓ |
| TC | Algorithm |
|---|---|
| O( |
DFS-combination( |
| O( |
DP |
| O( |
DP, Floyd-Warshall |
| O( |
DP |
| O(nlogn) | Sorting, Heap, divide&conquer, Dijkstra-heap, QuickSort |
| O(n) | DP, DFS-tree(V), BFS(V+E), TopologicalSorting(V+E), BucketSort(N+K), MonotonicStack |
| O(logn) | BinarySearch, BinaryIndexTree |
| O(1) | Math |
- What is the data size?
- Can I sort or group the elements?
- Can I use DP, greedy or binary search?
- Can I enumerate on a specific variable?
- Can I use two passes?
- Can I solve it reversely?
- Can I convert the problem to another problem?
- "Subarray" → consider sliding window, monotonic stack/queue, prefix sum + hash table, Kadane's algorithm.
Efficiently find all the palindrome numbers in a range 10**9:
pal = []
base = 1
while base <= 10000:
# odd number
for i in range(base, base * 10):
x = i
t = i // 10
while t:
x = x * 10 + t % 10
t //= 10
pal.append(x)
# even number
if base <= 1000:
for i in range(base, base * 10):
x = t = i
while t:
x = x * 10 + t % 10
t //= 10
pal.append(x)
base *= 10
pal.append(1_000_000_001) # sentinel