Skip to content

Coding patternsBlind 75

Blind 75, solved step by step

The 75 classic coding interview problems, each solved from the simple way to the fast way, with a step-through, code in five languages and the Big O worked out from the code.

Arrays and hashing 8

  1. 1Contains DuplicateA set remembers what you have seen, so a repeat shows up in one pass.Easy
  2. 2Valid AnagramTwo words are anagrams when every letter appears the same number of times.Easy
  3. 3Two SumFor each number, ask a map whether its partner has already gone past.Easy
  4. 4Group AnagramsGive every anagram the same key, then group the words by that key.Medium
  5. 5Top K Frequent ElementsCount each number, then pick the k biggest counts without a full sort.Medium
  6. 6Encode and Decode StringsWrite each string's length before it, so any character can appear inside.Medium
  7. 7Product of Array Except SelfEverything except i is everything left of i times everything right of i.Medium
  8. 8Longest Consecutive SequenceOnly start counting from a number that begins a run.Medium

Two pointers 3

  1. 9Valid PalindromeWalk in from both ends and skip anything that is not a letter or digit.Easy
  2. 103SumSort, fix one number, then find the other two with two pointers.Medium
  3. 11Container With Most WaterTwo lines hold water up to the shorter one. Drop the shorter line each step.Medium

Sliding window 4

  1. 12Best Time to Buy and Sell StockKeep the cheapest price so far and check each day's profit against it.Easy
  2. 13Longest Substring Without Repeating CharactersGrow the window, and shrink it from the left the moment a letter repeats.Medium
  3. 14Longest Repeating Character ReplacementA window is fine while its length minus its most common letter is at most k.Medium
  4. 15Minimum Window SubstringGrow until the window covers every letter, then shrink while it still does.Hard

Stack 1

  1. 16Valid ParenthesesEvery closing bracket must match the most recent open one: a stack.Easy

Binary search 2

  1. 17Find Minimum in Rotated Sorted ArrayCompare the middle with the right end to see which half holds the drop.Medium
  2. 18Search in Rotated Sorted ArrayOne half is always sorted. Check if the target is in it, then halve.Medium

Linked list 6

  1. 19Reverse Linked ListTurn each arrow around as you walk, keeping hold of the next node.Easy
  2. 20Merge Two Sorted ListsAlways take the smaller head, and a dummy node removes the special cases.Easy
  3. 21Linked List CycleA fast and a slow pointer meet if and only if there is a loop.Easy
  4. 22Reorder ListFind the middle, reverse the second half, then weave the two halves.Medium
  5. 23Remove Nth Node From End of ListStart one pointer n steps ahead, so the other stops right before the target.Medium
  6. 24Merge K Sorted ListsA min-heap of the k heads always knows which node comes next.Hard

Trees 11

  1. 25Invert Binary TreeSwap the children of every node. Recursion visits each one once.Easy
  2. 26Maximum Depth of Binary TreeA tree is one level deeper than its deeper subtree.Easy
  3. 27Same TreeTwo trees match when their roots match and both pairs of subtrees match.Easy
  4. 28Subtree of Another TreeAt every node of the big tree, ask Same Tree.Easy
  5. 29Lowest Common Ancestor of a BSTWalk down from the root until the two values split to different sides.Medium
  6. 30Binary Tree Level Order TraversalA queue visits the tree one level at a time.Medium
  7. 31Validate Binary Search TreePass down the range each node must fall in, not just its parent's value.Medium
  8. 32Kth Smallest Element in a BSTAn in-order walk of a BST visits values from smallest to largest.Medium
  9. 33Construct Binary Tree from Preorder and Inorder TraversalPreorder names the root; inorder tells you what goes left and right.Medium
  10. 34Binary Tree Maximum Path SumEach node hands its parent its best one-sided path and checks the path that bends at it.Hard
  11. 35Serialize and Deserialize Binary TreeWrite the tree in preorder with markers for empty spots, then read it back the same way.Hard

Tries 3

  1. 36Implement Trie (Prefix Tree)One node per letter, shared by every word that starts the same way.Medium
  2. 37Design Add and Search Words Data StructureA trie, where a dot tries every child.Medium
  3. 38Word Search IIPut the words in a trie, then walk the grid and the trie together.Hard

Heap 1

  1. 39Find Median from Data StreamKeep the smaller half in a max-heap and the larger half in a min-heap.Hard

Backtracking 2

  1. 40Combination SumChoose a number, recurse, undo. Never go back to earlier numbers, so no repeats.Medium
  2. 41Word SearchTry each cell as a start, then step to neighbors, marking the path as you go.Medium

Graphs 7

  1. 42Number of IslandsEach time you find new land, flood all of it, and count one island.Medium
  2. 43Clone GraphWalk the graph and keep a map from each old node to its copy.Medium
  3. 44Pacific Atlantic Water FlowFlow uphill from each ocean instead, and keep the cells both oceans reach.Medium
  4. 45Course ScheduleYou can finish every course when the prerequisites have no cycle.Medium
  5. 46Graph Valid TreeA tree has exactly n minus 1 edges and every node is connected.Medium
  6. 47Number of Connected Components in an Undirected GraphUnion-find merges groups as edges arrive, and counts the groups left.Medium
  7. 48Alien DictionaryNeighboring words give letter orders; a topological sort puts them in line.Hard

1-D dynamic programming 10

  1. 49Climbing StairsThe ways to reach a step are the ways to reach the two steps below it.Easy
  2. 50House RobberAt each house, either rob it and skip the last one, or skip it.Medium
  3. 51House Robber IIThe first and last house touch, so solve House Robber twice without one of them.Medium
  4. 52Longest Palindromic SubstringEvery palindrome grows out from a center. Try each center.Medium
  5. 53Palindromic SubstringsExpand from every center and count each palindrome on the way out.Medium
  6. 54Decode WaysThe ways to decode up to i come from the last one digit or the last two.Medium
  7. 55Coin ChangeThe fewest coins for an amount is one more than the best amount you can reach it from.Medium
  8. 56Maximum Product SubarrayTrack the largest and the smallest product, because a negative can swap them.Medium
  9. 57Word BreakA prefix can be split when a shorter split prefix plus one dictionary word makes it.Medium
  10. 58Longest Increasing SubsequenceFor each number, the longest run ending there builds on smaller numbers before it.Medium

2-D dynamic programming 2

  1. 59Unique PathsThe paths into a cell are the paths into the cell above plus the cell to the left.Medium
  2. 60Longest Common SubsequenceMatch the last letters if you can; otherwise drop one letter from one word.Medium

Greedy 2

  1. 61Maximum SubarrayDrop the running sum the moment it goes negative: it can only hurt.Medium
  2. 62Jump GameKeep the farthest index you can reach; you are stuck if you ever pass it.Medium

Intervals 5

  1. 63Insert IntervalCopy what ends before, merge what overlaps, copy what starts after.Medium
  2. 64Merge IntervalsSort by start, then each interval either extends the last one or starts a new one.Medium
  3. 65Non-overlapping IntervalsKeep the interval that ends first; it leaves the most room for the rest.Medium
  4. 66Meeting RoomsSort by start; any meeting that starts before the last one ends is a clash.Easy
  5. 67Meeting Rooms IIA min-heap of end times tells you when a room frees up.Medium

Math and geometry 3

  1. 68Rotate ImageA quarter turn is a flip across the diagonal, then a flip of each row.Medium
  2. 69Spiral MatrixWalk the outer ring, shrink the four borders, and repeat.Medium
  3. 70Set Matrix ZeroesUse the first row and column as the notebook of which rows and columns to clear.Medium

Bit manipulation 5

  1. 71Number of 1 Bitsn and (n minus 1) clears the lowest 1 bit, so count how often you can do it.Easy
  2. 72Counting BitsA number has the same bits as half of it, plus its last bit.Easy
  3. 73Reverse BitsTake bits off the right end of n and push them onto the right end of the answer.Easy
  4. 74Missing NumberXOR every index and every value; pairs cancel and the missing one is left.Easy
  5. 75Sum of Two IntegersXOR adds without carries, AND finds the carries; repeat until no carry is left.Medium

More in Coding patterns