Skip to content
dsa450for five year olds

Patterns

Learn the tricks, not just the problems.

Interviewers rarely ask a problem you have seen. They ask one that uses a trick you have seen. Here every problem is grouped by the trick that cracks it.

Pattern matching

Slide a small word over a big one, and skip ahead smartly when it does not match.

Reach for it when: KMP, Rabin Karp, Z, Boyer Moore.

6 problems

Two pointers

Two fingers walking along a row, often from both ends toward the middle.

Reach for it when: Sorted arrays, pairs that add up to something, reversing, palindromes.

32 problems

Fast and slow pointers

A tortoise and a hare on the same track. If there is a loop, the hare laps the tortoise.

Reach for it when: Loops in linked lists, the middle of a list, repeated numbers.

8 problems

Sliding window

A picture frame that slides along a row, adding on one side and dropping on the other.

Reach for it when: Best stretch of k items, the shortest stretch that has everything.

10 problems

Prefix sums

A running total written under each locker, so any stretch is one subtraction away.

Reach for it when: Sums of ranges, subarrays that add to a target.

13 problems

Hash map counting

A notebook where you tally what you have seen, so you never look twice.

Reach for it when: Duplicates, pairs, anagrams, first unique thing.

25 problems

Binary search

Open the book in the middle and throw away the half you do not need.

Reach for it when: Anything sorted, or any yes and no question that flips once.

24 problems

Sorting first

Line everyone up by height first. Many problems become easy once things are in order.

Reach for it when: Closest pairs, merging, scheduling, grouping.

36 problems

Grid walking

Walk a chocolate bar square by square: along rows, down columns, in a spiral, or down a staircase.

Reach for it when: Matrices: spirals, rotations, searching a sorted grid, scanning in all directions.

6 problems

Kadane

Walk the row with a bag of points. If the bag goes negative, drop it and start fresh.

Reach for it when: Best sum or product of a stretch of numbers.

3 problems

Monotonic stack

A pile where shorter kids step out when a taller kid arrives, so everyone finds who is taller next to them.

Reach for it when: Next greater or smaller, histograms, stock spans.

7 problems

Stacks and queues

A pile of plates and a lunch line.

Reach for it when: Brackets, undo, expressions, processing in arrival order.

38 problems

Linked list rewiring

Unhook and rehook train cars without dropping any.

Reach for it when: Reversing, merging, rotating, removing from lists.

16 problems

Tree recursion

Ask the left child, ask the right child, and combine their answers.

Reach for it when: Heights, sums, paths, checks on trees.

24 problems

Breadth first search

Fan out ring by ring with a waiting line, like ripples in a pond.

Reach for it when: Rows of a tree, shortest number of steps, spreading things (rotting oranges).

9 problems

Depth first search

Follow one road as far as it goes, then walk back and try the next.

Reach for it when: Islands, connected parts, cycles, all paths.

34 problems

Shortest paths

Always grow the closest town next, and fix up the distances as you go.

Reach for it when: Weighted maps: Dijkstra, Bellman Ford, Floyd Warshall.

4 problems

Union find

Every kid points to a team captain. Joining teams is pointing one captain at another.

Reach for it when: Grouping, connecting cables, spanning trees, cycles in undirected graphs.

4 problems

Heap

A line where the smallest kid is always at the front, however you add people.

Reach for it when: Top k, merging k sorted things, running medians, scheduling.

22 problems

Greedy

Grab the best looking choice now and never look back. Works only when you can prove it is safe.

Reach for it when: Intervals, coins with nice values, fractional knapsack.

15 problems

Backtracking

Explore a maze with a ball of string. Dead end? Follow the string back and try the next way.

Reach for it when: All permutations, subsets, boards, mazes.

26 problems

Trie

A tree of letters where words with the same start share a branch.

Reach for it when: Prefixes, autocomplete, word lists.

6 problems

Dynamic programming

Write each small answer in a chart so you never do the same homework twice.

Reach for it when: Counting ways, best totals, comparing two strings.

58 problems

Bit tricks

Numbers are rows of light switches. Flip, test and combine switches fast.

Reach for it when: Single numbers, powers of two, subsets as masks.

9 problems

Math and counting

A neat formula that skips the work.

Reach for it when: Factorials, Catalan numbers, combinations, number puzzles.

10 problems

Other techniques

2 problems