Algorithm Patterns

Updated: 2023-03-07
12 min read



def insertion_sort(array):
    for i in range(1, len(array)):
        value = array[i]
        while i > 0 and array[i - 1] > value:
            array[i] = array[i - 1]
            i -= 1
            array[i] = value
    return array
def selection_sort(array):
    for i in range(len(array) - 1):
        min_value = i
        for j in range(i + 1, len(array)):
            if array[j] < array[min_value]:
                min_value = j
        temp = array[min_value]
        array[min_value] = array[i]
        array[i] = temp
    return array
def find_target(nums, target):
    left = 0
    right = len(nums) - 1
    while left <= right:
        mid = (left + right) // 2
        if nums[mid] == target:
            return mid

        if nums[mid] < target:
            left = mid + 1
            right = mid - 1
    return -1

Python build-in module

from bisect import bisect_left

sorted_fruits = ['apple', 'banana', 'orange', 'plum']
bisect_left(sorted_fruits, 'kiwi')

>> 2

Dynamic programming (DP)

Breadth First Search (BFS)

BFS on Tree:

def bfs(root):
    queue = deque([root])
    while len(queue) > 0:
        node = queue.popleft()
        for child in node.children:
            if is_goal(child):
                return FOUND(child)
    return NOT_FOUND

BFS on Graph:

def bfs(root):
    queue = deque([root])
    visited = set([root])
    while len(queue) > 0:
        node = queue.popleft()
        for neighbor in get_neighbors(node):
            if neighbor in visited:

BFS on a Matrix:

num_rows, num_cols = len(grid), len(grid[0])
def get_neighbors(coord):
    row, col = coord
    delta_row = [-1, 0, 1, 0]
    delta_col = [0, 1, 0, -1]
    res = []
    for i in range(len(delta_row)):
        neighbor_row = row + delta_row[i]
        neighbor_col = col + delta_col[i]
        if 0 <= neighbor_row < num_rows and 0 <= neighbor_col < num_cols:
            res.append((neighbor_row, neighbor_col))
    return res

from collections import deque

def bfs(starting_node):
    queue = deque([starting_node])
    visited = set([starting_node])
    while len(queue) > 0:
        node = queue.popleft()
        for neighbor in get_neighbors(node):
            if neighbor in visited:
            # Do stuff with the node if required
            # ...

Depth-first search (DFS)

DFS on Tree:

def dfs(root, target):
    if root is None:
        return None
    if root.val == target:
        return root
    left = dfs(root.left, target)
    if left is not None:
        return left
    return dfs(root.right, target)

DFS on Graph:

def dfs(root, visited):
    for neighbor in get_neighbors(root):
        if neighbor in visited:
        dfs(neighbor, visited)

DFS on two-dimensional array:

Let’s imagine you have a big maze made of walls and corridors, and you want to find a way from the entrance to the exit. You can put a robot at the entrance, and you want to tell the robot what to do to find the exit.

The first thing you might tell the robot is to always go as far as it can in one direction before turning. This is what depth-first search does.

The robot starts at the entrance and goes as far as it can down the first corridor it finds.

  • If it comes to a dead end, it goes back to the last intersection it passed and tries the next corridor.
  • If it comes to the exit, it stops and says “I found the exit!”.


# Define the maze as a two-dimensional array
maze = [
  ['.', '.', '#', '#', '#', '#', '#', '#'],
  ['#', '.', '.', '.', '#', '.', '.', '#'],
  ['#', '.', '#', '.', '#', '.', '.', '#'],
  ['#', '.', '.', '.', '.', '#', '.', '#'],
  ['#', '#', '#', '#', '.', '#', '.', '#'],
  ['#', '.', '.', '.', '.', '.', '.', '#'],
  ['#', '.', '#', '#', '#', '#', '.', '.'],
  ['#', '#', '#', '#', '#', '#', '#', '.'],

# Define the starting point and the destination
start = (0, 0)
end = (len(maze)-1, len(maze[0])-1)

# Define a function to find the exit using depth-first search
def dfs(current, visited):
  # Mark the current cell as visited

  # Base case: If we've reached the destination, return True
  if current == end:
    return True

  # Try all possible directions from the current cell
  for delta in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
    next_cell = (current[0] + delta[0], current[1] + delta[1])
    if is_valid_cell(next_cell) and next_cell not in visited:
      if dfs(next_cell, visited):
        return True

  # If we couldn't find the exit from this cell, backtrack to the previous cell
  return False

# Call the depth-first search function with the starting point and an empty set of visited cells
visited = set()
if dfs(start, visited):
  print("I found the exit!")
  print("I couldn't find the exit.")

Sliding Window

Usage: Use when need to handle the input data in specific window size.


Example: Sliding window technique to find the largest sum of 4 consecutive numbers.


while j < size:
    # Calculation's happen's here
    # ...

    if condition < k:
    elif condition == k: # ans <-- calculation
    elif condition > k:
        while condition > k:
            i+=1 # remove calculation for i
return ans


Problem: Find the largest sum of K consecutive entries, given an array of size N

  1. Add the first K components together and save the result in the currentSum variable. Because this is the first sum, it is also the current maximum; thus, save it in the variable maximumSum.
  2. As the window size is ww, we move the window one place to the right and compute the sum of the items in the window.
  3. Update the maximum if the currentSum is greater than the maximumSum, and repeat step 2.
def max_sum(arr, k):
	n = len(arr)    # length of the array

	# length of array must be greater
        # window size
	if n < k:
		return -1

	# sum of first k elements
	window_sum = sum(arr[:k])
	max_sum = window_sum

	# remove the  first element of previous
	# window and add the last element of
	# the current window to calculate the 
    # the sums of remaining windows by
	for i in range(n - k):
		window_sum = window_sum - arr[i] + arr[i + k]
		max_sum = max(window_sum, max_sum)

	return max_sum

arr = [16, 12, 9, 19, 11, 8]
k = 3
print(max_sum(arr, k))

Problem: Find duplicates within a range ‘k’ in an array

Input: nums = [5, 6, 8, 2, 4, 6, 9]
k = 2
Ouput: False
def get_duplicates(nums, k):
    d = {}
    count = 0
    for i in range(len(nums)):
        if nums[i] in d and i - d[nums[i]] <= k:
            return True
            d[nums[i]] = i
    return False

Two Pointers

A classic way of writing a two-pointer sliding window. The right pointer keeps moving to the right until it cannot move to the right (the specific conditions depend on the topic). When the right pointer reaches the far right, start to move the left pointer to release the left boundary of the window.

Usage: Use two pointers to iterate the input data. Generally, both pointers move in the opposite direction at a constant interval.



Based on Depth-first search (DFS)


Finding all permutations, combinations, subsets and solving sudoku are classic combinatorial problems.

Imagine you are trying to solve a puzzle, like a Sudoku. When you are solving a puzzle, sometimes you reach a point where you can’t make any more progress using the current path. That’s when you need to backtrack.

Backtracking is a general algorithmic technique that is used to find all (or some) solutions to a problem by incrementally building candidates, and checking if the candidate is feasible or not. If the candidate is not feasible, the algorithm goes back (backtracks) to the previous step and tries again with a different candidate. The process continues until a solution is found, or all candidates have been tried.

Backtracking is an algorithmic technique for solving problems recursively by trying to build a solution incrementally, one piece at a time, removing those solutions that fail to satisfy the constraints of the problem at any point of time.

Backtracking algorithm is derived from the Recursion algorithm, with the option to revert if a recursive solution fails, i.e. in case a solution fails, the program traces back to the moment where it failed and builds on another solution. So basically it tries out all the possible solutions and finds the correct one.

Backtracking == DFS on a tree


  1. Backtracking is drawing tree
  2. When drawing the tree, bear in mind:
    • how do we know if we have reached a solution?
    • how do we branch (generate possible children)?


Let’s say we want to generate all possible combinations of 1, 2, and 3 of length 2. The possible combinations are: (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2).

This process generates all possible combinations of length k:

def backtrack(nums, path, res, k):
    # nums: the list of available numbers
    # path: the current path of selected numbers
    # res: the list of all valid combinations
    # k: the length of each combination
    if len(path) == k: # base case

    for i in range(len(nums)):
        backtrack(nums[:i] + nums[i+1:], path, res, k)

nums = [1, 2, 3]
k = 2
res = []
backtrack(nums, [], res, k)
# [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]


  1. We start with an empty path and empty result list.
  2. We loop through the available numbers (1, 2, 3) and add the first number to the path.
  3. We make a recursive call to backtrack with the remaining numbers (2, 3) and a path that includes the first number (e.g., [1]). This adds all possible combinations of length k-1 with the first number.
  4. After the recursive call, we remove the first number from the path.
  5. We repeat this process for the other available numbers, generating all possible combinations of length k.
  6. When we reach the base case (len(path) == k), we add the current path to the result list.
  7. We return the result list of all possible combinations.

The base case is when the length of the path is equal to k. At this point, we add the current path to the result list and return.

The recursive case involves looping through the available numbers, adding the current number to the path, making a recursive call with the remaining numbers, and removing the current number from the path after the recursive call.


In the backtrack function, path refers to the list of numbers that have been selected so far to form a valid combination.

Initially, path is an empty list []. In each recursive call, a number from nums is selected and added to path.

For example, if nums = [1, 2, 3] and the current path is [1], the function will call backtrack([2, 3], [1], res, k) to consider all possible combinations with 1 in the first position, followed by all possible combinations of length k-1 of [2, 3] in the second position.

Once all possible combinations with 1 in the first position have been explored, the number 1 will be removed from path, and the function will try the next number from nums, which in this case is 2. The function continues in this way until all valid combinations of length k have been found and added to the res list.

Problem examples:

Example of LeetCode 78 problem:


We define a backtrack function named backtrack(first, curr) which takes the index of first element to add and a current combination as arguments.

  1. If the current combination is done, we add the combination to the final output.

  2. Otherwise, we iterate over the indexes i from first to the length of the entire sequence n.

    1. Add integer nums[i] into the current combination curr.
    2. Proceed to add more integers into the combination: backtrack(i + 1, curr).
    3. Backtrack by removing nums[i] from curr.
class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        def backtrack(first = 0, curr = []):
            # if the combination is done
            if len(curr) == k:  
            for i in range(first, n):
                # add nums[i] into the current combination
                # use next integers to complete the combination
                backtrack(i + 1, curr)
                # backtrack
        output = []
        n = len(nums)
        for k in range(n + 1):
        return output

# [    [], 
#     [1], [3], [4], 
#     [1, 3], [1, 4], [3, 4], 
#     [1, 3, 4]
# ]

Dutch National Flag problem

The Dutch National Flag problem is a sorting problem that asks us to sort an array of colors, like a bunch of different colored socks. We want to put all the socks of the same color together in the array.

The colors in this problem are represented by numbers. We use the numbers 0, 1, and 2 to represent the colors red, white, and blue. So, we have an array of numbers, and we want to sort them in such a way that all the 0's are at the beginning of the array, then all the 1's, and finally all the 2's are at the end.

For example, if we have an array [2, 0, 2, 1, 1, 0], we want to sort it so that it becomes [0, 0, 1, 1, 2, 2].

One way to solve this problem is to use a technique called the Dutch National Flag algorithm. The idea behind this algorithm is to use three pointers: a low pointer, a mid pointer, and a high pointer.

The low pointer starts at the beginning of the array, the high pointer starts at the end of the array, and the mid pointer starts at the beginning of the array.

We then iterate through the array with the mid pointer.

  • If the value at the mid pointer is 0, we swap it with the value at the low pointer and increment both pointers. - If the value at the mid pointer is 1, we leave it where it is and just increment the mid pointer.
  • If the value at the mid pointer is 2, we swap it with the value at the high pointer and decrement the high pointer.

We keep doing this until the mid pointer passes the high pointer, at which point the array is sorted.

So, in our sock example, we start with the low pointer at the beginning of the array, the mid pointer also at the beginning of the array, and the high pointer at the end of the array. Then, we iterate through the array with the mid pointer, swapping socks as needed until the array is sorted by color.


Previous Plan
Next Helpers