DSA: Pattern Recognition

Level up your problem-solving skills by identifying common patterns in data structures and algorithms for coding challenges.

Explore Patterns
author

Femi Adigun

Senior Software Engineer & Coach

Updated March 18, 2025

Mastering common coding patterns is a game changer—it helps you recognize underlying structures in problems, making them quicker to understand and solve. Below is a breakdown of the essential coding patterns you should focus on.

Essential Coding Patterns

🧩
1. Two-Pointer Technique

When to use:

  • Arrays or strings where you need to compare elements from both ends or move through them with two indices.
  • Problems like sorting, searching pairs, or removing duplicates.

Common scenarios:

  • Finding a pair with a target sum in a sorted array
  • Checking if a string is a palindrome
  • Merging two sorted arrays

Example:

Time Complexity: O(n)
Arrays
Strings
Optimization
📏
2. Sliding Window

When to use:

  • Problems involving contiguous sequences (subarrays, substrings) with maximum, minimum, or target sums/lengths.

Common scenarios:

  • Maximum sum of a subarray of size k
  • Longest substring without repeating characters
  • Smallest window containing all characters of another string

Example:

Time Complexity: O(n)
Arrays
Strings
Subarrays
🔄
3. Fast and Slow Pointers

When to use:

  • Detecting cycles in linked lists or arrays
  • Finding middle elements or cycle lengths

Common scenarios:

  • Detecting a cycle in a linked list (Floyd's cycle detection)
  • Finding the start of the cycle in a sequence

Example:

Time Complexity: O(n)
Linked Lists
Cycle Detection
🧠
4. Dynamic Programming

When to use:

  • Problems with overlapping subproblems and optimal substructure (solvable from solutions to smaller subproblems).

Common scenarios:

  • Fibonacci numbers (optimized)
  • Longest common subsequence
  • Knapsack problems
  • Coin change (minimum number of coins)

Example:

Time Complexity: O(n)
Optimization
Memoization
Tabulation
🌲
5. Recursion and Backtracking

When to use:

  • Problems requiring exploration of all possible solutions (e.g., permutations, combinations).

Common scenarios:

  • N-Queens problem
  • Generating subsets or permutations
  • Maze-solving problems
  • Sudoku solver

Example:

Time Complexity: O(2^n)
Combinatorial
Search
🧮
6. Binary Search

When to use:

  • Sorted arrays or search spaces where a decision can be made based on comparisons.

Common scenarios:

  • Finding an element in a sorted array
  • Searching for boundaries (first/last occurrences)
  • Finding square roots or peaks in arrays

Example:

Time Complexity: O(log n)
Search
Divide & Conquer
📊
7. Prefix Sums

When to use:

  • Range-based sum or frequency queries.

Common scenarios:

  • Sum of elements between two indices quickly
  • Range update queries in arrays

Example:

Time Complexity: Query Time: O(1)
Arrays
Range Queries
🌳
8. Tree and Graph Traversals

When to use:

  • Navigating hierarchical or networked data structures.

Common scenarios:

  • Finding shortest paths (BFS)
  • Counting connected components
  • Tree-based problems (lowest common ancestor, diameter of a tree)

Example:

Time Complexity: O(V + E)
Trees
Graphs
Pathfinding
💧
9. Greedy Algorithms

When to use:

  • Problems where making a locally optimal choice leads to a global solution.

Common scenarios:

  • Interval scheduling (activity selection)
  • Coin change (with standard coin sets)
  • Huffman coding

Example:

Time Complexity: O(n log n)
Optimization
Scheduling
⚙️
10. Bit Manipulation

When to use:

  • Working with integers at the binary level for optimization.

Common scenarios:

  • Checking if a number is a power of two
  • Finding a single non-duplicate number (XOR tricks)
  • Counting set bits

Example:

Time Complexity: O(1) for isPowerOfTwo
Binary
Optimization

How to Master These Patterns

1

Pick one pattern per week to study and practice.

2

Solve 3-5 problems per pattern on platforms like LeetCode, HackerRank, or CodeSignal.

3

Write down the approach and note how the pattern applies.

4

Compare your solution to others for optimization techniques.