Skip to content
Algorithms for Beginners

Patterns

Almost every coding problem is one of these 19 patterns in disguise. Learn to spot the clue, and you know where to start.

  1. Hashing and counting

    Remember what you have seen in a Map, so you never search for it twice.

    Spot it when: You need to find a match, a duplicate, or count how often things appear.

  2. Two pointers

    Walk through a list from both ends (or at two speeds) at once.

    Spot it when: The list is sorted, or you compare items from the start and the end.

  3. Sliding window

    Keep a stretch of the list and slide it along instead of starting over.

    Spot it when: You want the best stretch of items next to each other.

  4. Binary search

    Halve the search space with every guess.

    Spot it when: The data is sorted, or the answer is a number you can test 'too big or too small'.

  5. Stack

    Keep the most recent unfinished thing on top, and finish it first.

    Spot it when: Things must match up in pairs, or you need the next bigger or smaller item.

  6. Prefix sums

    Add up running totals once, then answer any range sum instantly.

    Spot it when: You are asked about sums of many different ranges.

    Coming soon

  7. Fast and slow pointers

    Move one pointer twice as fast as the other to find loops and middles.

    Spot it when: A linked list might loop back on itself, or you need its middle.

    Coming soon

  8. Linked list tricks

    Rewire nodes in place: reverse, merge, and remove.

    Spot it when: The input is a linked list and you must change its order.

    Coming soon

  9. Tree traversal

    Visit every node of a tree, depth first or level by level.

    Spot it when: The input is a tree, or the problem has a parent-child shape.

    Coming soon

  10. Heaps and top K

    Always have the smallest (or biggest) item ready to take.

    Spot it when: You need the K biggest, K smallest, or the next item by priority.

    Coming soon

  11. Intervals

    Sort ranges by their start, then merge or compare neighbours.

    Spot it when: The input is a list of start and end times.

    Coming soon

  12. Backtracking

    Try a choice, explore everything after it, then undo it and try the next.

    Spot it when: You must list every combination, arrangement or path.

    Coming soon

  13. Graph search

    Explore connected things with breadth-first or depth-first search.

    Spot it when: Things are connected to each other: grids, maps, networks.

    Coming soon

  14. Topological sort

    Put tasks in an order where everything comes after what it depends on.

    Spot it when: Some things must happen before others.

    Coming soon

  15. Union-find

    Track which items belong to the same group as groups merge.

    Spot it when: You keep joining items and asking whether two are connected.

    Coming soon

  16. Dynamic programming

    Build the answer from answers to smaller versions of the problem, saved in a table.

    Spot it when: You count ways or find the best total, and choices overlap.

    Coming soon

  17. Greedy

    Take the best-looking choice at each step, when that is provably safe.

    Spot it when: A simple local rule (earliest, cheapest, biggest) seems to always work.

    Coming soon

  18. Tries

    Store words letter by letter so shared beginnings are stored once.

    Spot it when: You search many words by their prefix.

    Coming soon

  19. Bit manipulation

    Use the ones and zeros inside numbers to solve problems with tiny memory.

    Spot it when: The problem mentions binary, or a trick with XOR would cancel pairs out.

    Coming soon