Coding Drills
100 hands-on Java problems, organised into 10 topics that build on each other. Each drill has the problem, the key insight, a step-by-step diagram, a complete compilable solution, complexity, and the bugs people usually write.
How the 10 Topics Build on Each Other
The order matters. Arrays and hashing teach you to trade memory for time. Two pointers and sliding window teach you to stop re-scanning data. Binary search teaches you to throw half the input away. Linked lists drill pointer handling, which trees then use recursively. Grids and graphs reuse the same DFS and BFS on a wider structure. Dynamic programming caches those recursions, and backtracking enumerates them. Work top to bottom the first time; after that, jump straight to whichever topic you're weakest in.
The 10 Topic Pages
Two Sum, Group Anagrams, Product Except Self, Subarray Sum = K, Majority Element, Stock I/II. HashMap complements, prefix sums, XOR, Boyer–Moore voting.
Reverse, Move Zeroes, Remove Duplicates, Merge Sorted, Rotate, Container With Most Water, Trapping Rain Water, Next Permutation. Opposite-end and read/write pointers.
Valid Parentheses, K-distinct substring, Character Replacement, Minimum Window Substring, Longest Palindrome, strStr, Multiply Strings. Variable windows, expand-around-center.
Search Insert, Peak Element, Rotated Minimum, 2D Matrix, Median of Two Sorted Arrays, Merge Intervals, Meeting Rooms. Lower-bound templates, partition search, sort-then-sweep.
Reverse, Merge, Cycle, Middle, Remove Nth, Add Two Numbers, Swap Pairs, Rotate, Odd/Even, Intersection, Partition. Dummy heads, fast/slow, pointer rewiring.
Traversals, Depth, Same/Symmetric, Balanced, Path Sum, Diameter, Level Order, Zigzag, LCA, Validate BST, Build from Pre+In, Flatten. Recursion contracts and BFS levels.
Flood Fill, Number of Islands, Max Area, Rotting Oranges, Shortest Path in Binary Matrix, Pacific Atlantic, Spiral Matrix. Grid DFS/BFS, multi-source BFS, reverse flow.
Clone Graph, Connected Components, Valid Tree, Course Schedule, Topological Sort, Accounts Merge, Word Ladder, Kruskal MST, Dijkstra. Union-Find, Kahn's algorithm, priority queues.
Climbing Stairs, House Robber, Coin Change, Decode Ways, Word Break, LIS, Max Product, Jump Game, Grid paths, Maximal Square, Subset Sum, LPS, Edit Distance. 1D, 2D and knapsack DP.
Permutations, Combination Sum, Generate Parentheses, Min Stack, LRU Cache, K Closest Points. Choose/explore/un-choose, auxiliary stacks, HashMap plus doubly-linked list, heaps.
How to Work Every Drill
Every drill page follows the same six steps, and your own practice should too. The most common mistake is jumping straight to code. A few minutes on examples and a brute force almost always reveal the optimal pattern, because the optimal solution is usually the brute force with one repeated piece of work removed.
Pattern Finder: From Problem Signal to Technique
Most drills give away their pattern in the problem statement. Train yourself to spot these signals in the first read. Each row links the signal to the technique and to the page that drills it.
Read the Constraints: Your Complexity Budget
A typical judge runs roughly 108 simple operations per second. The constraint on n tells you which complexity is acceptable, and that in turn narrows down which patterns are even worth trying.
| Max n | Target | Typical technique | Example drills |
|---|---|---|---|
| n ≤ 10 | O(n!) | Permutations, full backtracking | 95 Permutations |
| n ≤ 20–25 | O(2n) | Subsets, combination search | 96 Combination Sum, 97 Parentheses |
| n ≤ 500 | O(n³) | Interval DP, triple loops | 93 Longest Palindromic Subseq. (n²) |
| n ≤ 104 | O(n²) | 2D DP, expand-around-center | 28 Palindrome, 86 LIS (n² version), 94 Edit Distance |
| n ≤ 106 | O(n log n) | Sort, heap, binary search | 38 Merge Intervals, 79 Kruskal, 100 K Closest |
| n ≤ 108 | O(n) | Hashing, two pointers, window | 1 Two Sum, 21 Rain Water, 27 Min Window |
| n > 108 | O(log n) / O(1) | Binary search, math | 33 Search Insert, 37 Median (log) |
The Java You'll Type Most
Most of the 100 solutions use the same dozen or so API calls. Know these without looking them up, and know their costs. For deeper coverage, see Java Collections for DSA.
Integer caching
Compare boxed Integer values with .equals(), not ==. == only works by accident for −128…127.
Overflow
lo + hi, products, and running sums overflow int. Use long or lo + (hi − lo) / 2.
Comparators
Use Integer.compare(a, b) instead of a − b. Subtraction overflows for large values of opposite sign.
A 4-Week Study Plan
| Week | Topics | Drills | Goal |
|---|---|---|---|
| Week 1 | 01 · 02 · 03 | 1–32 | Get O(n) to be your default: hash maps, pointers, windows |
| Week 2 | 04 · 05 · 06 | 33–64 | Correct loop bounds, pointer rewiring, recursion contracts |
| Week 3 | 07 · 08 | 65–80 | DFS/BFS from memory, topological sort, Union-Find, Dijkstra |
| Week 4 | 09 · 10 | 81–100 | Define the state, recurrence and base case before writing any code |
- Read the problem and example only. Close the solution and give yourself 20 minutes.
- Write the brute force first, even if it's obvious. Then state what it repeats.
- Compare your answer with the walkthrough diagram, not just the final code.
- Re-solve from a blank file 3 days later. If you can't, it wasn't learned yet.
- Track each drill as solved / solved-with-hint / failed, and redo failures at the end of the week.