Coding Patterns: A Comprehensive Guide

6 minute read

Published:

This post includes some popular problems sorted according to the different patterns. This list excludes tree or graph related BFS and DFS, and dynamic programming. I feel that those topics require their own posts!

  • Backtracking
  • Greedy
  • Bit Manipulation
  • Two Pointers
  • Trie
  • 2 Heaps
  • Cyclic Sort
  • In-place reversal of LinkedList
  • K way merge
  • Merge Intervals
  • Sliding Window
  • Binary Search
  • Monotonic Stack

Greedy Algorithmic Paradigm

Greedy algorithms make locally optimal choices at each step with the hope of finding a global optimum. They are often used when a problem can be solved by making a series of decisions that look best in the short term.

Fixed Sliding Window

Sliding Window problems are useful when you need to calculate something over a range of elements in an array or string. In the fixed sliding window pattern, the window size is constant.

Variable Sliding Window

In the variable sliding window pattern, the window size can change dynamically depending on the conditions of the problem.

Top K elements

Top K Elements problems typically require finding the Kth largest or smallest elements in a data structure. These problems are often solved using heaps, which allow for efficient extraction of the Kth largest or smallest elements.

K-Way merge

K-Way Merge problems typically involve merging K sorted lists or arrays. These problems are commonly solved using a min-heap, which allows for efficiently merging sorted data structures.

Merge Intervals

Merge Intervals problems involve working with intervals or ranges. The goal is often to merge overlapping intervals or find a range that satisfies specific criteria. These problems are generally solved by first sorting the intervals and then iterating through them.

Cyclic Sort

Cyclic Sort is a pattern that involves sorting a list of numbers that range from 1 to n. The approach is based on placing each number at its correct index (i.e., number i at index i-1). This pattern is efficient for finding missing numbers, duplicates, or corrupt numbers in a given range.

In-Place Reversal of LinkedList

In-Place Reversal of LinkedList is a pattern where the linked list is reversed or modified without using extra space (in place). This pattern is useful for reversing a sublist, rotating the linked list, or detecting and removing cycles in the list.

Bit Manipulation

Bit Manipulation is a powerful technique used to solve problems by directly manipulating bits. This pattern is often used in problems involving sets, subsets, and certain arithmetic operations.

Binary Search is a pattern that involves dividing the search space in half with each step, typically used in sorted arrays or search problems. It’s a highly efficient way to solve problems with logarithmic time complexity.

Monotonic Stack

Monotonic Stack is a pattern where you maintain a stack with elements that are either entirely non-increasing or non-decreasing. This technique is useful for problems that require tracking the nearest larger or smaller elements.

Two Pointers

Two Pointers is a pattern that uses two pointers to traverse an array or list. It is often used in problems where you need to find pairs of elements that satisfy a certain condition, such as finding the pair of numbers that add up to a given sum.

Trie

A Trie is a specialized tree-like data structure used to store associative arrays where the keys are usually strings. Tries are used for efficient information retrieval, such as prefix searching.

2 Heaps

The 2 Heaps pattern is useful for solving problems where you need to find the median of a data stream or maintain the smallest/largest K elements in a dataset. Typically, a min-heap and a max-heap are used together.

Backtracking

Backtracking is a technique for solving problems recursively by building a solution incrementally, one piece at a time, and removing those solutions that fail to satisfy the problem’s constraints.