Open Chapter Overview

Forward

Patterns You Should Know

Next Module

It’s simply not worth your time to know how to solve every coding problem. That would be like taking a History class and memorizing the date of every event the professor mentions. Instead, you’ll want to be comfortable with the main patterns; if you understand all of them, you’ll be able to solve almost any problem that is reasonable to ask during an assessment or interview.

This article provides more detail and example use cases for each pattern, so feel free to check it out if a pattern is especially confusing to you.

The Patterns

  • Sliding Window: perform an operation on a subset of an array, then shift to the right.
  • Two Pointers: move two pointers through an array, which is often more efficient than using a single pointer.
  • Binary Search: using two pointers (‘start’ and ‘end’), find a ‘middle’ element and test a condition. Use the result of that operation to determine which half of the remaining array to check further.
  • Fast and Slow: move two pointers, one faster than the other. The interactions between the two can be helpful for working with Linked Lists.
  • Backtracking: solve by trial and error, returning to previous steps when mistakes happen
  • Intervals: handle segments, time periods, or windows that overlap (or don’t)
  • Swap Sort: rearrange elements of an array to make it conform to some condition.
  • In-Place Reversal of Linked List: reverse one node at a time using multiple pointers to ensure the list stays intact.
  • Breadth First Search: use a tree traversal algorithm that visits the root, then the node’s children in order.
  • Depth First Search: use a tree traversal algorithm that visits the node’s children, then the root.
  • Subsets: find all possible sub-combinations of elements from an array (a subset is an array that results from deleting any number of elements, including none, from the original array)
  • Top K Elements: use a Heap to keep track of elements in an array.
  • Two Heaps: use a Min Heap and a Max Heap that work together to find certain elements in a set.
  • K-Way Merge: use a Heap to combine elements from various lists in order
  • Topological Sort: find a dependent order of nodes in a Directed Acyclic Graph