Coding interview patterns
Interview questions are new problems built on a few old ideas. Each pattern comes with a picture walkthrough, a code template and the clues that tell you to use it.
- Two Pointers
Two indexes walk the array from both ends (or at two speeds) instead of checking every pair. - Binary Search
Halve a sorted range every step: O(log n) instead of O(n). - Frequency Counting (Hashing)
Count how often each value appears in one pass, then answer from the counts. - Recursion and Base Cases
Solve a problem by calling the same function on a smaller input, stopping at a base case. - Running Total (Accumulator)
Carry a sum, product or count forward as you loop. - Digit Manipulation (% and /)
Peel digits off a number with % 10 and / 10. - Track the Best So Far
One pass that remembers the largest, smallest or best value seen. - Assume, Then Disprove (Flag and Early Exit)
Assume the answer is yes, look for a counter-example, and stop as soon as you find one. - Nested Loops
An outer loop for rows or passes, an inner loop for columns or comparisons. - Compare and Swap (Sorting)
Compare neighbours or a chosen element and swap until the order is right. - Matrix Traversal
Visit a grid row by row with indexes [i][j].